List<String> nameList = new List<String>();
DropDownList ddl = new DropDownList();
列表在这里填充,然后排序:
nameList.Sort();
现在我需要将它放到下拉列表中,这是我遇到问题的地方(使用 foreach):
foreach (string name in nameList){
ddl.Items.Add(new ListItem(nameList[name].ToString()));
}
没有工作人员 - 有什么建议吗?它给我编译错误:
Error - The best overloaded method match for 'System.Collections.Generic.List<string>.this[int]' has some invalid arguments
Error - Argument '1': cannot convert from 'string' to 'int'
最佳答案
为什么不像直接将 DDL 绑定(bind)到列表
DropDownList ddl = new DropDownList();
ddl.DataSource = nameList;
ddl.DataBind();
关于C# - 将列表转储到下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/247856/