当“主”下拉列表更改时,如何重新填充“依赖”下拉列表?

时间:2017-04-13 20:47:26

标签: asp.net vb.net html-select selectedindex selectedindexchanged

我有三个下拉列表,其中第二个的内容取决于第一个中选择的内容,第三个的内容取决于第二个中选择的内容。这在首次显示页面时有效:

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

...但我需要第二个下拉列表在第一个下拉列表的选择发生变化时更新,第三个下拉列表在第二个下拉列表的选择发生变化时进行更新。我已经有了这个代码来尝试实现这个目标:

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

...但它不适用于豆子小山 - 可能是因为事件可能不会被解雇。无论如何,它看起来似乎并非如此 - 因为DropDownListMembers和DropDownListCustomers都没有被清除,DropDownListMembers也没有被一组不同的项重新填充。

所以我的问题是,当一个人改变时,我怎样才能重新填充“依赖”下拉列表,但也许问题应该是“我如何才能触发SelectedIndexChanged()事件?

1 个答案:

答案 0 :(得分:1)

事件未触发,因为AutoPostBack属性未设置为true。将其设置为true将确保每当用户更改所选项时,它将触发SelectedIndexChanged处理程序。