How do I load a dropdown list in asp.net and c#?
+1
A:
wow...rather quick to the point there...
DropDownLists have an items collection, you call the Add method of that collection.
DropDownList1.Items.Add( "what you are adding" );
keithwarren7
2008-12-17 03:52:08
+5
A:
You can also do it declaratively:
<asp:DropDownList runat="server" ID="yourDDL">
<asp:ListItem Text="Add something" Value="theValue" />
</asp:DropDownList>
You can also data bind them:
yourDDL.DataSource = YourIEnumberableObject;
yourDDL.DataBind();
DaRKoN_
2008-12-17 03:55:51
you can also add programmatically myDropDownList.Items.Add( "stuff");
JohnIdol
2008-12-17 09:31:42
A:
If you have a collection of employee objects, you could add them like so:
List<Employee> ListOfEmployees = New List<Employees>();
DropDownList DropDownList1 = new DropDownList();
foreach (Employee employee in ListOfEmployees) {
DropDownList1.Items.Add(employee.Name);
}
George Stocker
2008-12-17 03:58:13
+3
A:
using Gortok's example, you can databind the list to the dropdownlist also
List<Employee> ListOfEmployees = New List<Employees>();
DropDownList DropDownList1 = new DropDownList();
DropDownList1.DataSource = ListOfEmployees ;
DropDownList1.DataTextField = "TextFieldToBeDisplayed";
DropDownList1.DataValueField = "ValueFieldForLookup";
DropDownList1.DataBind();
awaisj
2008-12-17 05:02:45