读书人

DropDownList 数据源绑定添加新项的有

发布时间: 2012-01-19 20:57:58 作者: rapoo

DropDownList 数据源绑定添加新项的问题 -- 新手-谢谢!!
我的DropDownList 指定了 数据源 ;但我想在DDL的第一个选项加一个“——请选择——”
请各位帮帮忙!!

[解决办法]

C# code
            DataTable dt = new DataTable();            DropDownList1.DataSource = dt;//dt是SQL查询出来的DataTable,绑定数据源              DropDownList1.DataTextField = "字段";            DropDownList1.DataValueField = "字段";            DropDownList1.DataBind();            DropDownList1.Items.Insert(0, new ListItem("——请选择——", "0"));//插入项
[解决办法]
this.ddl.Items.Insert(0,new ListItem("--请选择---","-1"));
[解决办法]
C# code
this.DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));
[解决办法]
如果不是写代码的话,不是很好做!
要自己写代码指—ropDownList 的数据源,不过在指定之前必须对数据加工
如Categorie 是一个类
CategotyBLL是三成中的一个类


public void GetDropDownListData()
{
IList<Categorie> list = CategotyBLL.GetAll();//重数据库中获得数据放在IList中, IList中都放的是类的实例
//new一个类,把它放到IList中
Categorie cate = new Categorie();
cate.Name = "--选择--";
cate.Id = -1;

list.Insert(0, cate);
//在将IList的对象和DropDownList1绑定
this.DropDownList1.DataSource = list;
this.DropDownList1.DataTextField = "Name";
this.DropDownList1.DataValueField = "Id";
this.DropDownList1.DataBind();
}
[解决办法]
DropDownList1.Items.Insert(0, new ListItem("——请选择——", "0"));//
[解决办法]
探讨
那 DropDownList1.Items.Insert(0, new ListItem("——请选择——", "0"));
与 DropDownList1.Items.Add(0, new ListItem("——请选择——", "0"));
有什么区别呢?

读书人网 >asp.net

热点推荐