DropDownList的数据绑定问题
做一个联系,使用DropDownList控件,我使用了一个方法创建了一个数据源
private ListItem[] sourceGet()
{
string[,] books = {
{"aaa","111"},
{"bbb","222"},
{"ccc","333"},
{"ddd","444"},
{"eee","555"},
{"fff","666"},
{"ggg","777"},
{"hhh","888"}
};
ListItem[] items = new ListItem[8];
for (int i = 0; i < books.GetLength(0); i++)
{
items[i] = new ListItem(books[i, 0], books[i, 1]);
}
return items;
}
if (!IsPostBack)
{
DropDownList1.DataSource = sourceGet();
DropDownList1.DataBind();
}
DropDownList1.DataBind();
[解决办法]
for (int i = 0; i < books.GetLength(0); i++)
{
DropDownList1.Items.Add(new ......
}
获取用Dictionary<string,string> dic=.......
DropDownList1.DataSource =dic
DropDownList1.DataTextField = "Kev";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
[解决办法]
var books = new[] {
new { Name = "a", Price = "100" } ,
new { Name = "b", Price = "1" }
};
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Price";
DropDownList1.DataSource = books;
DropDownList1.DataSource();