ASP.NET 两个Dropdownlist控件联动

两个dropdownlist联动网上代码很多,原理也很简单,这里不再重复了,贴下我自己的代码,希望对给位有帮助(主要关联是在第一个dropdownlist空间的selectindexchanged事件)

第一个控件绑定:

Private Sub display()
Dim cmd As New DataBean ‘我是用的数据库连接类
Dim ds As New DataSet
Dim i As Integer
Dim str As String = "SQL语句"

cmd.ConnOracle()
ds = cmd.GetDataSet(str)
cmd.CloseConn()


If ds.Tables(0).Rows.Count > 0 Then
Me.drpplant.Items.Add("")
For i = 0 To ds.Tables(0).Rows.Count - 1
Me.drpplant.Items.Add(ds.Tables(0).Rows(i).Item(0).ToString)
Next
Else
Me.lblMsg.Text = "Not Found Any Plant Data!"
End If

Me.btnstop.Enabled = False
Me.List1.Items.Clear()
Me.List2.Items.Clear()
End Sub

第二个控件内容绑定
Private Sub downlocation(ByVal plant As String)
Dim cmd As New DataBean
Dim ds As New DataSet
Dim i As Integer
Dim str As String = "select wiplocation from cpi_wip_location where plant='" & plant & "' order by id asc"

cmd.ConnOracle()
ds = cmd.GetDataSet(str)
cmd.CloseConn()

If ds.Tables(0).Rows.Count > 0 Then
For i = 0 To ds.Tables(0).Rows.Count - 1
Me.drploc.Items.Add(ds.Tables(0).Rows(i).Item(0).ToString)
Next
End If

End Sub

第一个空间的联动事件:

Protected Sub drpplant_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpplant.SelectedIndexChanged
Me.drploc.Items.Clear()
Call downlocation(Me.drpplant.SelectedValue.ToString.Trim)
End Sub

原文地址:https://www.cnblogs.com/kevinhome/p/2730397.html