Search code examples
asp.netvb.nethtml-selectselectedindexselectedindexchanged

How can I repopulate "dependent" dropdownlists when the "master" dropdownlist changes?


I have three dropdown lists, where the contents of the second depend on what is selected in the first, and the contents of the third depend on what is selected in the second. This works when the page is first shown:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If PageAlreadyInitted = True Then Exit Sub
    If Page.IsPostBack = True Then Exit Sub
    PageAlreadyInitted = True

    Dim sqlDAL As New SQLServer(ConfigurationSettings.AppSettings("ConnectionString"))
    'Populate the Units dropdown
    Dim unitsDT As DataTable
    Dim sql As String = "Select distinct mu.unit from masterunits mu left join MasterUnitsProjSales mps on mps.Unit = mu.Unit where abs(active) = 1 and mps.NewBiz != 0 order by mu.unit"
    Dim retDS As DataSet = sqlDAL.runSQLDataSet(sql)
    unitsDT = retDS.Tables(0)
    DropDownListUnits.DataSource = unitsDT
    DropDownListUnits.DataTextField = "unit"
    DropDownListUnits.DataValueField = "unit"
    DropDownListUnits.DataBind()

    'Populate the Members dropdown - TODO: Call this when Unit changes, too
    Dim selectedUnit As String = DropDownListUnits.SelectedItem.Text
    Dim membersDT As DataTable
    sql = "select distinct shortname, M.memberno from members M left join memberunitproducts mup on M.MemberNo = mup.MemberNo where unit = '" + selectedUnit + "' order by shortname"
    retDS = sqlDAL.runSQLDataSet(sql)
    membersDT = retDS.Tables(0)
    DropDownListMembers.DataSource = membersDT
    DropDownListMembers.DataTextField = "shortname"
    DropDownListMembers.DataValueField = "memberno"
    DropDownListMembers.DataBind()

    'Populate the Customers dropdown - TODO: Call this when Member changes, too
    Dim selectedMember As String = DropDownListMembers.SelectedItem.Value
    Dim customersDT As DataTable
    sql = "select distinct companyname, custno from customers C left join members M on M.MemberNo = C.MemberNo where M.MemberNo = '" + selectedMember + "' order by companyname"
    retDS = sqlDAL.runSQLDataSet(sql)
    customersDT = retDS.Tables(0)
    DropDownListCustomers.DataSource = customersDT
    DropDownListCustomers.DataTextField = "companyname"
    DropDownListCustomers.DataValueField = "custno"
    DropDownListCustomers.DataBind()

    sqlDAL.Dispose()
End Sub

...but I need the second dropdown to update when the selection of the first one changes, and the third one to update when the selection of the second changes. I've got this code to try to accomplish that:

Protected Sub DropDownListUnits_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownListUnits.SelectedIndexChanged
    DropDownListMembers.Items.Clear()
    DropDownListCustomers.Items.Clear()

    'Dim selectedUnit As String = DropDownListUnits.SelectedItem.Text
    Dim selectedUnit As String = DropDownListUnits.SelectedItem.ToString()
    Label2.Text = selectedUnit

    Dim sqlDAL As New SQLServer(ConfigurationSettings.AppSettings("ConnectionString"))
    Dim membersDT As DataTable
    Dim retDS As DataSet
    Dim sql As String = "select distinct shortname, M.memberno from members M left join memberunitproducts mup on M.MemberNo = mup.MemberNo where unit = '" + selectedUnit + "' order by shortname"
    retDS = sqlDAL.runSQLDataSet(sql)
    membersDT = retDS.Tables(0)

    DropDownListMembers.DataSource = membersDT
    DropDownListMembers.DataTextField = "shortname"
    DropDownListMembers.DataValueField = "memberno"
    DropDownListMembers.DataBind()
End Sub

...but it doesn't work worth a hill of beans - possibly because the event may not be firing. It sure seems as if it isn't, anyway - that being apparent because neither DropDownListMembers nor DropDownListCustomers are cleared, nor is DropDownListMembers repopulated with a different set of items.

So my question is, how can I repopulate "dependent" dropdownlists when one changes, but perhaps the question really should be, "How can I get the SelectedIndexChanged() event to fire?


Solution

  • The events are not firing because the AutoPostBack property isn't set to true. Setting it to true will ensure whenever the user changes the selected item that it will fire the SelectedIndexChanged handler.