Populate ASP.Net DropDownList by Adding Items Dynamically

Updated on 04 Jul 2012,
Published on 28 Oct 2009

The ASP.Net DropDownList control also provides the method to populate it by adding the list items dynamically using C# code. You can use the Add method of Items collection property of DropDownList control to populate the items programmatically. The Items property gets the collection of list items stored in the DropDownList control. It belongs to the ListItemCollection class and provides the read-only member property of this collection class. The Items collection property further enables you to access the public methods of ListItemCollection class that allow you to add or remove the list items. Here we will use the Add method to populate the list items into the DropDownList control.

Following are the two overloaded ways for using the Add method of Items collection property of DropDownList:

1.

[DropDownList].Items.Add( ListItem item );

It allows you to add the specified list item at the end of DropDownList’s Item collection. You can use the well suited overloaded method of ListItem class constructor to add a new item.

2.

[code:c#][DropDownList].Items.Add( string text );

It appends the collection of list items of DropDownList control by adding the specified string at the end of items collection.

For adding the ListItem using first overloaded Add method you can use the following overloaded constructor of ListItem class:

new ListItem( string text, string value );

It allows you to add the ListItem with its text that will appear in the DropDownList control along with its value that will work at the back end.

Sample Code to Populate DropDownList Control Dynamically

HTML Code
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
C# Code

There are different ways to add the List Items into the DropDownList control. In the following samples we have shown 2 different ways to add the ListItems:

Example 1
ListItemCollection itemsCollection = DropDownList1.Items;

itemsCollection.Add(new ListItem("item 1", "1"));
itemsCollection.Add(new ListItem("item 2", "2"));
itemsCollection.Add(new ListItem("item 3", "3"));
itemsCollection.Add(new ListItem("item 4", "4"));
itemsCollection.Add(new ListItem("item 5", "5"));
Example 2
DropDownList1.Items.Add(new ListItem("item 1", "1"));
DropDownList1.Items.Add(new ListItem("item 2", "2"));
DropDownList1.Items.Add(new ListItem("item 3", "3"));
DropDownList1.Items.Add(new ListItem("item 4", "4"));
DropDownList1.Items.Add(new ListItem("item 5", "5"));

You can build your logic to add the ListItems into the ListItemCollection of DropDownList control using any of the above C# code. You can also use C# for loop to add the items that will also reduce the code lines.

Output:

Get the output for above discussed code from the following link:

Populate DropDownList Items Dynamically

Continue to the next tutorial: Setting ASP.Net Dropdownlist Default Value to learn how to select a list item to display it as default value of dropdownlist control.

0 Responses to "Populate ASP.Net DropDownList by Adding Items Dynamically"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email