by Santhakumar Munuswamy
Posted on 30 十一月 2014
ASP.NET
下拉列表控件:
它使用户可以从单选下拉列表中进行选择。下拉列表包含"n" number of items. DropDownList控件还支持数据绑定,例如将控件绑定到数据源的数据,例如对象数据源,xml数据源和sql数据源,其中包含要在控件中显示的项目。该DropDownList控件可用于手动添加数据,甚至可以动态添加数据库中的数据绑定。
数据绑定方法
这是将数据源绑定到DropDownList控件的方法
DataTextField,DataValueField属性
指定要绑定到控件中每个列表项的“文本”和“值”属性的数据源中的哪个字段。
SelectedIndex属性
SelectedIndex属性,以编程方式确定用户从DropDownList控件中选择的项目的索引
Dropdownlist.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dropdownlist.aspx.cs" Inherits="Dropdownlist" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Asp.net dropdownlist control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="height: 45px"><asp:Label ID="Label1" runat="server" Style="left: -1px; position: relative; top: 0px"
Text="Name:" Width="46px"></asp:Label></td>
<td style="height: 45px"><asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="EmpName" DataValueField="EmpName" Height="26px" Style="left: 3px;
position: relative; top: 0px" Width="125px">
<asp:ListItem> << Select >></asp:ListItem>
</asp:DropDownList></td>
<td style="height: 45px">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
SelectCommand="SELECT [EmpName] FROM [tbl_employee_profile]"></asp:SqlDataSource></td>
</tr>
</table>
<asp:DropDownList ID="DropDownList" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
来自我的旧博客的来源: 阅读更多