Solved

ASP.NET Listbox

Posted on 2009-07-14
18
291 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:richard_gar
  • 7
  • 4
  • 3
  • +1
18 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24847317
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
 

Author Comment

by:richard_gar
ID: 24847330
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
 
LVL 16

Expert Comment

by:sunithnair
ID: 24847369
Have you put this code in Page_Load inside IsPostBack like this?

If Not IsPostBack Then
'Your code
EndIf
0
 
LVL 4

Expert Comment

by:udaydidigam
ID: 24847398
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
 

Author Comment

by:richard_gar
ID: 24847414
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
 
LVL 16

Expert Comment

by:sunithnair
ID: 24847468
Are you setting the SelectedItem somewhere else on the page?
0
 
LVL 4

Expert Comment

by:udaydidigam
ID: 24847486
put the auto post back property true for listbox control
0
 

Author Comment

by:richard_gar
ID: 24847507
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:sunithnair
ID: 24847575
I dont see a problem with your code. It should work as expected.
0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24847622
Can you post the aspx code (not the aspx.cs)?
0
 

Author Comment

by:richard_gar
ID: 24848645
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
 
LVL 4

Expert Comment

by:udaydidigam
ID: 24848978
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
 

Author Comment

by:richard_gar
ID: 24849451
I have copied your code and it is still not working for me.
0
 

Author Comment

by:richard_gar
ID: 24849746
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
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 24887180
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
 

Author Comment

by:richard_gar
ID: 24893036
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
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 500 total points
ID: 24896880
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now