我该如何在asp.net和c#中加载下拉列表?
<asp:DropDownList runat="server" ID="yourDDL">
<asp:ListItem Text="Add something" Value="theValue" />
</asp:DropDownList>
yourDDL.DataSource = YourIEnumberableObject;
yourDDL.DataBind();
编辑:如评论中所述,您也可以通过编程方式添加项目:
yourDDL.Items.Add(YourSelectListItem);
List<Employee> ListOfEmployees = New List<Employees>();
DropDownList DropDownList1 = new DropDownList();
DropDownList1.DataSource = ListOfEmployees ;
DropDownList1.DataTextField = "TextFieldToBeDisplayed";
DropDownList1.DataValueField = "ValueFieldForLookup";
DropDownList1.DataBind();
哇...直截了当地讲解了...
DropDownLists
有一个items集合,您可以调用该集合的Add方法。
DropDownList1.Items.Add( "what you are adding" );
List<Employee> ListOfEmployees = New List<Employees>();
DropDownList DropDownList1 = new DropDownList();
foreach (Employee employee in ListOfEmployees) {
DropDownList1.Items.Add(employee.Name);
}