tags:

views:

1219

answers:

4

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
+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_
you can also add programmatically myDropDownList.Items.Add( "stuff");
JohnIdol
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
+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