当 "master" 下拉列表更改时,如何重新填充 "dependent" 下拉列表?
How can I repopulate "dependent" dropdownlists when the "master" dropdownlist changes?
我有三个下拉列表,其中第二个的内容取决于第一个中选择的内容,第三个的内容取决于第二个中选择的内容。这在页面首次显示时有效:
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 也没有重新填充一组不同的项目。
所以我的问题是,当下拉列表发生变化时,我如何重新填充 "dependent",但也许真正的问题应该是,“我怎样才能触发 SelectedIndexChanged() 事件?
事件未触发,因为 AutoPostBack 属性 未设置为 true。将其设置为 true 将确保每当用户更改所选项目时,它将触发 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 也没有重新填充一组不同的项目。
所以我的问题是,当下拉列表发生变化时,我如何重新填充 "dependent",但也许真正的问题应该是,“我怎样才能触发 SelectedIndexChanged() 事件?
事件未触发,因为 AutoPostBack 属性 未设置为 true。将其设置为 true 将确保每当用户更改所选项目时,它将触发 SelectedIndexChanged 处理程序。