locked
problems selecting from listbox RRS feed

  • Question

  • User945405123 posted

    hi everyone. i have a web form containing 2 listbox and 1 label. the first listbox gets its data from a database. the second listbox gets its data from the database also but it depends on the user selection from the first listbox. Label shows the user selection like this listbox1.item > listbox2.item. both listbox binded successfully to the database but when i choose an item from listbox 2 it keeps selecting the first one automatically no matter what i choose in listbox 2. and label shows the first item on the list box 2 only below are the codes. thank you in advance

    <td style="width:33px;" valign = "top" >
                    
                        <asp:ListBox ID = "lstCategories" runat = "server" SelectionMode = "Single" Rows = "10" Width = "250" AutoPostBack = "true" />
                    </td>
                    <td style="width:33px;" valign = "top">
                        <asp:ListBox ID = "lstSubCategories" runat = "server" SelectionMode = "single" Rows = "10" Width = "250" AutoPostBack = "true" Visible= "False" />
                    </td>
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
    
                Dim myConnection As New SqlConnection(myConStr)
                Dim mySqlCommand As New SqlCommand()
                Dim mySqlDataReader As SqlDataReader
                mySqlCommand.Connection = myConnection
                mySqlCommand.CommandText = "SELECT fldCategory_ID, fldCategory_Name FROM tblCategories WHERE fldCategory_ID > 2"
    
    
                myConnection.Open()
                mySqlDataReader = mySqlCommand.ExecuteReader
                'mySqlDataReader.Read()
                lstCategories.Items.Clear()
                lstCategories.DataValueField = "fldCategory_ID"
                lstCategories.DataTextField = "fldCategory_Name"
                'ddlCat.DataValueField = "fldCategory_ID"
                'ddlCat.DataTextField = "fldCategory_Name"
                'ddlCat.DataSource = mySqlDataReader
                'ddlCat.DataBind()
                lstCategories.DataSource = mySqlDataReader
                lstCategories.DataBind()
                lstCategories.SelectedIndex = -1
    
                lblChoice.Text = "Choose the category of item you want to sell."
                'Response.Write(DateTime.Now.ToString())
            End If
        End Sub
    
        Protected Sub lstCategories_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCategories.SelectedIndexChanged
            lblChoice.Text = lstCategories.SelectedItem.Text & "  >"
            lstSubCategories.Visible = True
    
            Dim myConnection As New SqlConnection(myConStr)
            Dim mySqlCommand As New SqlCommand()
            Dim mySqlDataReader As SqlDataReader
            mySqlCommand.Connection = myConnection
            mySqlCommand.CommandText = "SELECT fldCategory_ID, fldSub_Category_Name FROM tblSubCategory WHERE fldCategory_ID = " & lstCategories.SelectedValue
    
    
            myConnection.Open()
            mySqlDataReader = mySqlCommand.ExecuteReader
            lstSubCategories.Items.Clear()
            'While mySqlDataReader.Read()
    
            lstSubCategories.DataValueField = "fldCategory_ID"
            lstSubCategories.DataTextField = "fldSub_Category_Name"
            lstSubCategories.DataSource = mySqlDataReader
            lstSubCategories.DataBind()
        End Sub
        Protected Sub lstSubCategories_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstSubCategories.SelectedIndexChanged
            ' If Not Page.IsPostBack Then
            lblChoice.Text &= lstSubCategories.SelectedItem.Text
            'tblDescription.Visible = True
            tblCondition.Visible = True
            lblDescription.Visible = True
            txtDescription.Visible = True
            txtDescription.Text = ""
    
            'End If
        End Sub
    Monday, October 24, 2011 4:05 AM

Answers

  • User1446927574 posted

    I see couple of problems in the code if the code behind you provided is complete.

    1. I dont see lstSubCategories Selected Index Changed event handled.

    2. The event for lstCategories_SelectedIndexChanged id not binded with Listbox with id lstCategories. it should be like OnSelectedIndexChanged="lstCategories_SelectedIndexChanged".

    Fixing above issues might solve your probem.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 24, 2011 9:09 AM

All replies