您的位置:首页 > 运维架构

DropDownList绑定数据源,获取值

2011-06-23 18:39 190 查看
DropDownList绑定数据源,获取值

DropDownList

1. 绑定数据

private void Initialization()

{

this.dropSpecialty.DataSource = sbn.Selete("select [id], specialtyname from specialty");

this.dropSpecialty.DataTextField = "specialtyname";

this.dropSpecialty.DataValueField = "ID";

this.dropSpecialty.DataBind();

this.dropSpecialty.Items.Insert(0, new ListItem("--请选择--", "-1"));

}

这里绑定专业表,在DropDownList中显示专业的名称,但是由于存放到班级表class中是specialtID,所以同时使用了DropDownList的DataTextField属性:显示专业名称

DataValueField属性:对应的专业编号

2. 当在列表中点击编辑后,跳转到更新页面,在DropDownList中显示该班级所在的专业

此时,应使用DropDownList的SeleteValue属性,

this.dropSpecialty.SelectedValue = cdt.SpecialtyID.ToString();

与class数据表中SpecialtyID对应,DropDownList会根据Value —> DataValueField -> DataTextField -> DropDownList中显示专业名称

3. 当对DropDownList进行数据绑定后,要想实现获取DropDownList中值必须设置AppendDataBoundItems属性为True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: