1. 前端绑定drowdownlist
a) 模板列datasource直接绑定
前端
<asp:TemplateField HeaderText="Certificate Type" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:DropDownList ID="ddlCertificateType" runat="server" DataSource='<%# BindDdl()%>' DataValueField="iItemID" DataTextField="cField1"></asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
后台
Protected Function BindDdl() As DataTable
Return tools.GetLookupList("CERTIFICATETYPE", "maintsort")
End Function
b) 数据源绑定
<asp:DropDownList ID="cboAccountType" Runat="server" AutoPostBack="True" Width="100%"
DataSourceID="oDSAccountType" DataTextField="cField1" DataValueField="cField2" >
</asp:DropDownList>
<asp:SqlDataSource ID="oDSAccountType" runat="server"
ConnectionString="<%$ ConnectionStrings:VOSConnectionString %>"
SelectCommand="VSP_Maintenance_GetItems" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="PAYSACCOUNT" Name="cCatCode" Type="String" />
<asp:Parameter DefaultValue="SORTORDER" Name="cOrderBy" Type="String" />
<asp:Parameter DefaultValue="0" Name="iNotForComboBoxes" Type="Int64" />
<asp:Parameter DefaultValue="1" Name="RtnAllField" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
c) 绑定数据
Private Sub BindRcbStudioManager()
Dim dtStudioManager As DataTable = (New StudioMgr).GetStudioManager(Session("iStudioID"))
Me.rcbstudioManager.DataSource = dtStudioManager
Me.rcbstudioManager.DataTextField = "studioManager"
Me.rcbstudioManager.DataValueField = "iStudioManagerID"
Me.rcbstudioManager.DataBind()
End Sub
2. Dropdownlist 特性
<asp:DropDownList ID="cboAccountType" Runat="server" AutoPostBack="True" Width="100%" > </asp:DropDownList>
如果AutoPostBack=True, 那么Postback时,添加的属性(Attributes)会丢失,因此添加属性的代码要写在IsPostback之外,
这时需要在Page_Load时判断当前dropdownlist的当前选择值,比如: ucPaysBank.ascx.vb
a) 循环添加数据及属性
Private Sub loadCboAccountType()
Dim oTblAccountType As DataTable = tools.GetLookupListAll("PAYSACCOUNT", "SORTORDER", 1, 1, 1)
cboAccountType.Items.Clear()
For Each oRow As DataRow In oTblAccountType.Rows
Dim item As New ListItem
item.Text = oRow("cField1").ToString
item.Value = oRow("iItemID").ToString
item.Attributes.Add("cField2", oRow("cField2").ToString)
cboAccountType.Items.Add(item)
Next
End Sub
b) 显示属性值
dim cField2 as string = cboAccountType.SelectedItem.Attributes.Item("cField2")
c) 添加值
cboPayment.Items.Clear()
cboPayment.Items.Add(New ListItem("", -1))
cboPayment.Items.Add(New ListItem("Final Payment", 1))
cboPayment.Items.Add(New ListItem("N/A", 2))
d) 取值
//获取value值
$("#ddlSubmodel").val();
//获取text值
$("#ddlSubmodel").find("option:selected").text();
4. Dropdown List可以多选,且出现Checkbox 需引入
<link href="../App_Themes/bootstrap/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Scripts/bootstrap/bootstrap-multiselect.js"></script>