public string Name
{
get { return name; }
set { name = value; id = 1; }
}
protected void Page_Load(object sender, EventArgs e)
{
List
{
new CatalogInfo ()
{
Name = "Option 1"
},
new CatalogInfo()
{
Name = "Option 2"
}
};
this.DropDownList1.DataSource = list;
this.DropDownList1.DataTextField = "Name";
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataBind();
}
假定你已经在控件属性上绑定了DataValueField和DataTextField。
你可以在cs代码中的Page_Load事件中添加:
if(!isPostback)
{
List
CategoryInfo info = new CategoryInfo(){ Id = 1, Name = '选项一'};
list.Add(info);
info = new CategoryInfo(){ Id = 2, Name = '选项二'};
list.Add(info);
ddl.DataSource= list;
ddl.DataBind();
}
dll是下拉框控件ID,以上模拟了数据源,你可以改成从数据库查询返回。
以上代码要在支持.net框架3.0以上.
DropDownList1.DataSource=List1;
DropDownList1.DataValueField="ID";
DropDownList1.DataTextField="Name";
DropDownList1.DataBind();
在页面加载事件中添加:
if(!IsPostBack)
{
dropdownlist.DataSource=查询的方法;
dropdownlist.DataTextField="name";
dropdownlist.DataValueField="Id";
dropdownlist.DataBind();
}
要顶的啊,楼主辛苦了,谢谢