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

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

问题描述

我已经找到了几个在互联网上的例子来做到这一点而真正努力得到它在VB的工作。 (试过,但转换的结果好坏参半)

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)

我需要DROPDOWNLIST的选择选项,以基于在所述第一下拉列表中的不同的值来填充。

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

任何人都可以用VB中的一个releativley简单的例子帮助吗?如果值是硬codeD的剧本没有大惊小怪。或SQL位,从表中提取数据

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

在此先感谢!

推荐答案

它做的方法是填充第二个下拉菜单中的第一个下拉列表的SelectedIndexChanged事件

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

举例

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

使用HTML源代码像这样:

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根据其他DropDownList的VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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