一、介绍
DropDownList控件用于创建下拉列表。支持数据绑定
二、属性
1、AutoPostBack属性:用于设置当改变选项内容时,,是否自动回送到服务器。True表示回送;False(默认)表示不回送。
2、DataSource属性:用于指定填充列表控件的数据源。
3、DataTextField属性:用于指定DataSource中的一个字段,该字段的值对应于列表项的Text属性。
4、 DataValueField属性:用于指定DataSource中的一个字段,该字段的值对应于列表项的Value属性。
5、Items属性:表示列表中各个选项的集合,如DropDownList.Items(i)
表示第i个选项,i从0开始。每个选项都有以下3个基本属性:
Text 属性:表示每个选项的文本。
Value属性:表示每个选项的选项值。
Selected属性:表示该选项是否被选中。
Count属性:通过Items.Count属性可获得DropDownList控件的选项数;
Add方法:通过items.Add方法可以向DropDownList控件添加选项;
Remove方法:通过items.Remove方法,可从DropDownList控件中删除指定的选项;
Insert方法:通过items.insert方法,可将一个新的选项插入到DropDownList控件中;
Clear方法:通过items.clear方法可以清空DropDownList控件中的选项。
6、SelectedIndex属性:用于获取下拉列表中选项的索引值。如果未选定任何项,则返回值-1(负1)。
7、SelectedItem属性:用于获取列表中的选定项。通过该属性可获得选定项的Text 和Value属性值。
8、SelectedValue属性:用于获取下拉列表中选定项的值。
9、 SelectedIndexchanged事件:当用户选择了下拉列表中的任意选项时,都将引发SelectedIndexChanged事件。
三、具体用法
1、添加类表
<asp:DropDownListID="DropDownList1" runat="server"Height="20px"
Width="96px" >
<asp:ListItem>河北</asp:ListItem>
<asp:ListItem>北京</asp:ListItem>
<asp:ListItem>上海</asp:ListItem>
</asp:DropDownList>
2、绑定数据列表
1.我们可以直接在控件上选择绑定数据源
2.也可以通过代码绑定
例如:添加各省的列表
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack )
{
SqlConnection con = newSqlConnection("server=.;database=BDQNLogin;uid=sa;pwd=123456");
//数据库连接
con.Open();//打开连接
SqlCommand cmd = newSqlCommand("select * from province",con); //执行的sql语句
SqlDataReader sdr =cmd.ExecuteReader();//数据读取
this.DropDownList1.DataSource =sdr;
this.DropDownList1.DataTextField = "proName"; //数据源中提供项文本的字段
this.DropDownList1.DataValueField = "proID";//数据源中提供项值的字段
this.DropDownList1.DataBind();//绑定
sdr.Close();
con.Close();
}
}
3、简单的联动
例如:我们在选择省后,自动关联该省的市,如下效果
在关联省的基础上,绑定市
//绑定市
SqlCommand cmdcity = newSqlCommand("select * from city where proID=" +this.DropDownList1.SelectedValue, con);
sdr = cmdcity.ExecuteReader();
this.DropDownList2.DataSource =sdr;
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataBind();
sdr.Close();
con.Close();
通过 DropDownList控件的SelectedIndexChanged事件(更改选定索引)触发
//选择省
protected voidDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = newSqlConnection("server=.;database=BDQNLogin;uid=sa;pwd=123456");
con.Open();
SqlCommand cmd = newSqlCommand("select * from city where proID=" +this.DropDownList1.SelectedValue,con);
SqlDataReader sdr =cmd.ExecuteReader();
this.DropDownList2.DataSource =sdr;
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataBind();
sdr.Close();
con.Close();
4、DropDownList可编辑
在做人事系统时因为需求需要DropDownList可绑定可编辑,
而在Web中DropDownList没有winfrom中DropDownList可编辑的属性,如果我们在DropDownList中添加新项更新到数据库,要实现此功能可通过如下方法。
方法1、添加textbox控件,如下效果
原理:在textbox中添加新内容后,通过textbox的SelectedIndexChanged将内容添加到DropDownList中,达到可绑定可写的目的。
(具体实现方法比较简单,不在说明)
方法二、通过html中的textbox和select控件来实现
<html>
<body>
<input id="txtTest"type="test" style="width:200px;POSITION:absolute; right: 846px;">
<select id="dropDownListTest"οnchange="document.getElementById('txtTest').value=this.options[this.selectedIndex].text"style="width:217px;CLIP: rect(auto auto auto 200px); POSITION: absolute;height:21px;">
<option>1--</option>
<option>2--</option>
</select>
</body>
</html>