ASP.NET Listbox

Morning,

I have a problem with the listbox control where I have the following code in a button but for some strange reason it will not place the selected item that I have clicked on into the textbox.

Any ideas?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        TextBox1.Text = "You Selected" & " " & ListBox1.SelectedItem.Text
 
    End Sub

Open in new window

richard_garAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Salim FayadCommented:
Can you post the code which is in the Page_Load event?
What I guess, is that there is some code on the Page_Load that is setting the TextBox1.Text value each time the page loads.
0
richard_garAuthor Commented:
here you go
        Dim conn As New SqlConnection(cp.pConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet("myDataSet")
        da.Fill(ds)
        ListBox1.DataSource = ds
        ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
        ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
        ListBox1.DataBind()

Open in new window

0
sunithnairCommented:
Have you put this code in Page_Load inside IsPostBack like this?

If Not IsPostBack Then
'Your code
EndIf
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

udaydidigamCommented:
put these statements in pageload event like this

If  Not Page.IsPastBack
{
Dim conn As New SqlConnection(cp.pConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet("myDataSet")
        da.Fill(ds)
        ListBox1.DataSource = ds
        ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
        ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
        ListBox1.DataBind()
}
0
richard_garAuthor Commented:
yeah I have done that and it is working but my textbox is always showing the first item in the list and not the others when I click on them
0
sunithnairCommented:
Are you setting the SelectedItem somewhere else on the page?
0
udaydidigamCommented:
put the auto post back property true for listbox control
0
richard_garAuthor Commented:
below is the whole code of the page.

Also I tried setting the postback to true and that makes no difference
Imports System.Data.SqlClient
Imports System.Data
 
 
Partial Class salesreport
    Inherits System.Web.UI.Page
 
    Dim cp As New Class1
    Dim cnnConnection As New SqlClient.SqlConnection(cp.pConnectionString)
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
 
            Dim conn As New SqlConnection(cp.pConnectionString)
            Dim cmd As New SqlCommand("SELECT * FROM tblSites", conn)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet("myDataSet")
            da.Fill(ds)
            ListBox1.DataSource = ds
            ListBox1.DataValueField = ds.Tables(0).Columns("companyid").ColumnName
            ListBox1.DataTextField = ds.Tables(0).Columns("sitename").ColumnName
            ListBox1.DataBind()
 
        End If
 
    End Sub
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Label1.Text = ListBox1.SelectedItem.Text
 
    End Sub
 
End Class

Open in new window

0
sunithnairCommented:
I dont see a problem with your code. It should work as expected.
0
Salim FayadCommented:
Can you post the aspx code (not the aspx.cs)?
0
richard_garAuthor Commented:
the html code in the form
<%@ Page Title="" Language="VB" MasterPageFile="~/Default.master" AutoEventWireup="false" CodeFile="salesreport.aspx.vb" Inherits="salesreport" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server">
                            <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
                            <br />
                            <br />
                            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                            <br />
                            <br />
                            <asp:Button ID="Button1" runat="server" Text="Button" />
                            <br />
</asp:Content>

Open in new window

0
udaydidigamCommented:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
 
            Dim conn As New SqlConnection(cp.pConnectionString)
            Dim cmd As New SqlCommand("SELECT companyid,sitename FROM tblSites", conn)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet("myDataSet")
            da.Fill(ds)
            ListBox1.DataSource = ds.Tables[0]
            ListBox1.DataValueField = "companyid"
            ListBox1.DataTextField = "sitename"
            ListBox1.DataBind()
 
        End If
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Label1.Text = ListBox1.SelectedItem.Text
 
    End Sub
 
End Class

This works fine for me

   

 
0
richard_garAuthor Commented:
I have copied your code and it is still not working for me.
0
richard_garAuthor Commented:
I have just setup another form with just that code and it is still not working for me.

I am using IE8 and I have checked with the compatability mode and still not working.

I am using .net 3.5 too
0
Salim FayadCommented:
I also tried it and it is working. But what I noticed is that you are using a MasterPage. Can you post the code of the Page_Load of the MasterPage? And if there is any code in the Page or MasterPage in the OnInit event?
0
richard_garAuthor Commented:
here is the code in the master page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        'If user has logged in to page direct then kick them to login page
        If IsNothing(Session("username")) Then
            Response.Redirect("login.aspx")
        End If
 
        lblDate.Text = System.DateTime.Now.ToLongDateString
        lblIP.Text = Request.UserHostAddress
        lblUser.Text = Session("username")
        lblLastLogin.Text = Session("lastlogin")
 
        Dim strSQL As String
        strSQL = "SELECT * FROM tblCompany WHERE companyid = '" & Session("companyid") & "'"
        Dim ds As New DataSet
        SQLSelect(ds, strSQL)
        lblCompany.Text = ds.Tables(0).Rows(0)("companyname")
        ds.Dispose()
 
        Dim strSQL1 As String
        strSQL1 = "SELECT * FROM tblSites WHERE companyid = '" & Session("companyid") & "'"
        Dim ds1 As New DataSet
        SQLSelect(ds1, strSQL1)
        lblSites.Text = ds1.Tables(0).Rows.Count
        ds1.Dispose()
 
    End Sub

Open in new window

0
Salim FayadCommented:
Rewrite it as following:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        'If user has logged in to page direct then kick them to login page
        If IsNothing(Session("username")) Then
            Response.Redirect("login.aspx")
        End If
 
 If Not IsPostBack Then
        lblDate.Text = System.DateTime.Now.ToLongDateString
        lblIP.Text = Request.UserHostAddress
        lblUser.Text = Session("username")
        lblLastLogin.Text = Session("lastlogin")
 
        Dim strSQL As String
        strSQL = "SELECT * FROM tblCompany WHERE companyid = '" & Session("companyid") & "'"
        Dim ds As New DataSet
        SQLSelect(ds, strSQL)
        lblCompany.Text = ds.Tables(0).Rows(0)("companyname")
        ds.Dispose()
 
        Dim strSQL1 As String
        strSQL1 = "SELECT * FROM tblSites WHERE companyid = '" & Session("companyid") & "'"
        Dim ds1 As New DataSet
        SQLSelect(ds1, strSQL1)
        lblSites.Text = ds1.Tables(0).Rows.Count
        ds1.Dispose()
 
 End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.