用List<string>如何绑定DropdownList

2024-12-05 05:08:23
推荐回答(3个)
回答1:

yissyo的应该可以!
既然有value,有text. 一个string怎么够呢! 你可以把value和text封装成一个类(class1)嘛! 对应的给个属性Value和Text!
List cList=new List();

ddl.DataSource = cList;
ddl.DataTextField = cList.Value;
ddl.DataValueField = cList.Text;
ddl.DataBind();

回答2:

List datas1 = new List();
List datas2 = new List();
datas1.Add("abc");
datas2.Add("123");
datas2.Add("1bd");

Dictionary dic = new Dictionary();
for (int i = 0; i < datas1.Count && i < datas2.Count; ++i)
{
dic.Add(datas1[i], datas2[i]);
}

DropDownList1.DataSource = dic;
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();

回答3:

List是一维数据完成不了你的任务,
考虑用Dictionary替代,

Dictionary dic = new Dictionary();
dic.Add("1", "Monday");
dic.Add("2", "Tuesday");

ddl.DataSource = dic;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();