如何使用vb.net根据另一个下拉列表值更改下拉值

时间:2018-07-05 10:58:49

标签: vb.net drop-down-menu dynamics-ax-2009

如何使用vb.net根据另一个下拉列表值更改下拉值

 <asp:DropDownList ID="DropDownList1" runat="server" 
  DataSourceID="SqlDataSource1" 
 DataTextField="Name" 
 DataValueField="Name"
  AppendDataBoundItems="True">
  <asp:ListItem>
  Select Site from list
   </asp:ListItem>
  </asp:DropDownList>
  Warehouse:
 <asp:DropDownList ID="DropDownList2" runat="server" 
  DataSourceID="SqlDataSource2" 
   DataTextField="NAME" 
  DataValueField="INVENTLOCATIONID"
  AppendDataBoundItems="True" >
<asp:ListItem>
 Select Warehouse from list
</asp:ListItem>
 </asp:DropDownList>

这是我的示例代码,如果我在第一个下拉列表相关仓库中选择站点需要显示在第二个下拉列表中

1 个答案:

答案 0 :(得分:0)

您可以使用第一个下拉菜单的SelectedIndexChanged事件。

Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As 
 EventArgs)
 Dim CountryID As Integer = Convert.ToInt32(ddlCountry.SelectedValue.ToString())
 FillStates(CountryID)
End Sub

Private Sub FillStates(ByVal countryID As Integer)
 Dim strConn As String = 
 ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString
 Dim con As New SqlConnection(strConn)
 Dim cmd As New SqlCommand()
 cmd.Connection = con
 cmd.CommandType = CommandType.Text
 cmd.CommandText = "Select StateID, State from State where CountryID =@CountryID"
 cmd.Parameters.AddWithValue("@CountryID", countryID)
 Dim objDs As New DataSet()
 Dim dAdapter As New SqlDataAdapter()
 dAdapter.SelectCommand = cmd
 con.Open()
 dAdapter.Fill(objDs)
 con.Close()
 If objDs.Tables(0).Rows.Count > 0 Then
   ddlState.DataSource = objDs.Tables(0)
   ddlState.DataTextField = "State"
   ddlState.DataValueField = "StateID"
   ddlState.DataBind()
   ddlState.Items.Insert(0, "--Select--")
Else
   lblMsg.Text = "No states found"
End If
End Sub