根据其他DropDownList VB填充DropdownList [英] Populate DropdownList based upon other DropDownList VB

查看:112
本文介绍了根据其他DropDownList VB填充DropdownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上找到了几个例子来做到这一点,但真的很努力地使其在VB中工作。 (试过一个转换器,但有不同的结果)



我需要根据第一个下拉列表中的不同值来填充一个下拉列表的选择选项。



任何人都可以在VB中帮助一个兼容性的简单例子?如果这些值在脚本中是硬编码的,那就不用担心。或者从表中提取数据的SQL位



提前感谢!

解决方案

完成的方式是在第一个下拉列表的SelectedIndexChanged事件中填充第二个下拉列表



示例

 code> 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作为新的SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.Command Text =Select StateID,State from State where CountryID = @ CountryID
cmd.Parameters.AddWithValue(@ CountryID,countryID)
Dim objD As New DataSet()
Dim dAdapter As新的SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
如果objDs.Tables 0).Rows.Count> 0然后
ddlState.DataSource = objDs.Tables(0)
ddlState.DataTextField =State
ddlState.DataValueField =StateID
ddlState.DataBind()
ddlState.Items.Insert(0,--Select--)
Else
lblMsg.Text =找不到状态
如果
End Sub

使用html源代码:

 < asp:DropDownList ID =ddlStaterunat =serverAutoPostBack =True> 
< / asp:DropDownList>

< asp:DropDownList ID =ddlCountryrunat =server
AutoPostBack =TrueOnSelectedIndexChanged =ddlCountry_SelectedIndexChanged>
< / asp:DropDownList>


I have found a couple of examples on the internet to do this but really struggling to get it working in VB. (Tried a converter but had mixed results)

I need the selection options of a Dropdownlist to be populated based upon the differing values in the first dropdown list.

Can anyone help with a releativley simple example in VB? Not fussed if the values are "hard coded" in the script. Or a SQL bit that pulls the data from a table

Thanks in advance!

解决方案

The way it is done is to populate second dropdown in SelectedIndexChanged event of the first dropdown

Example:

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

With html source as so:

   <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True">
  </asp:DropDownList>

  <asp:DropDownList ID="ddlCountry" runat="server" 
  AutoPostBack="True" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
 </asp:DropDownList>

这篇关于根据其他DropDownList VB填充DropdownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆