参考博客:https://blog.csdn.net/hejingyuan6/article/details/16847119
第一种:使用<asp:ListItem>方法
<asp:DropDownList id="weste" runat="server"> <asp:ListItem Value="0">小学</asp:ListItem> <asp:ListItem Value="1">中学</asp:ListItem> <asp:ListItem Value="2">大学</asp:ListItem> </asp:DropDownList>
第二种:使用Items属性的Add方法(mysql数据 作为 数据源)
通用方法:
#Region "给下拉列表设置数据源" Public Shared Sub setBindDropListDataSourseNew(ByVal dt As DataTable, ByVal ddlDropList As ExDropDownList, ByVal value As String, ByVal name As String) If ddlDropList.Items.Count > 0 Then ddlDropList.Items.Clear() End If If Not dt Is Nothing Then ddlDropList.Items.Add(New ListItem("", "")) For i As Integer = 0 To dt.Rows.Count - 1 ddlDropList.Items.Add(New ListItem(dt.Rows(i)(name), dt.Rows(i)(value))) Next End If End Sub #End Region
方法setBindDropListDataSourseNew(ByVal dt As DataTable, ByVal ddlDropList As ExDropDownList, ByVal value As String, ByVal name As String)
参数1:绑定的数据源 datatable类型
参数2:给哪个控件绑定,控件Id
参数3:下拉列表的value值,对应数据库的列名 classno
参数4:下拉列表的text值,对应数据库的列名 classname
ASP代码:
<asp:DropDownList ID="ddl_class1" runat="server" IsEnterToTab="true" OnSelectedIndexChanged="ddl_class1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList ID="ddl_class2" runat="server" IsEnterToTab="true" AutoPostBack="true">
</asp:DropDownList>
VB代码:
Protected Sub ddl_class1_SelectedIndexChanged(sender As Object, e As EventArgs) Utils.Util.setBindDropListDataSourseNew(dbaComm.getClass2(ddl_class1.SelectedValue), ddl_class2, "class2", "classname") End Sub
Public Class DBACommFnc
Public Shared dbaComm As New DBACommFnc
#Region "查询分类" Public Function getClass2(ByVal class1 As String) As DataTable Dim p_cmd As String = "SELECT m14.`class2`,m14.`classname` FROM m14 WHERE 1 = 1 AND m14.`class1` = '" & class1 & "' AND m14.`class2` <> '000'AND m14.`class3` = '000';" Dim dt As DataTable = dba.fncSelect(p_cmd) Return dt End Function #End Region