How Do I Add an Additional Item To a Databound DropDownList Control in ASP.NET?

It's a common practice to bind some data to a DropDownList or ListBox control. The data for these controls can come from a wide variety of DataSources including Arrays, ArrayLists, HashTables and DataSets. Quite often, though, you'll have the need to add an additional item to the beginning of the list.

This item often instructs your user to select an item from the list, as you can see in the following screenshot:

A drop-down list with the first item instructing the user to select an item

The list with countries is preceded by an item with the text Please select a country. Since this item is usually not present in the datasource, you'll have to add it either manually programmatically through some code.

ASP.NET 1.X

In ASP.NET 1.x you have to manually add the item after you have data bound your control. That's because a call to DataBind wipes out all existing items. The following code is used to bind the drop-down to an ArrayList of ListItems, each holding the ISO code and description for a country. As soon as the list is bound, an additional item is added to the list using the Insert method of the Items collection. The Items collection is defined in the ListControl class, the abstract base class for all list controls like the DropDownList, the ListBox, the RadioButtonList etc. The Insert method allows you to insert a new ListItem at a location you specify in the first parameter. In this example, I want the custom ListItem added at the very top of the list with countries. Since the Items collection of the drop down control is zero based, I have to pass 0 for this parameter.

// In the following code, lstCountries is a 
// System.Web.UI.WebControls.DropDownList control


// Get an ArrayList with Countries, and bind to lstCountries
lstCountries.DataSource = GetCountries();
lstCountries.DataValueField = "Value";
lstCountries.DataTextField = "Text";
lstCountries.DataBind();

// At this point, the DropDownlist is bound to the 
// ArrayList with Country ListItem objects

// Now add the custom ListItem at the top.
lstCountries.Items.Insert(0, new ListItem("Please select a country", ""));
The GetCountries method just returns an ArrayList with ListItem objects. Each ListItem object holds the ISO code for the country as a Value, and the description for the country as the Text.
public static ArrayList GetCountries()
{
  ArrayList countries = new ArrayList();
	
  countries.Add(new ListItem("Afghanistan", "af"));
  countries.Add(new ListItem("Albania", "al"));
  countries.Add(new ListItem("Algeria", "dz"));
  countries.Add(new ListItem("American Samoa", "as"));
  countries.Add(new ListItem("Andorra", "ad"));
  countries.Add(new ListItem("Angola", "ao"));
  countries.Add(new ListItem("Anguilla", "ai"));
  countries.Add(new ListItem("Antarctica", "aq"));
  // .... rest of countries here
  return countries;
}

In a real-life application, it's a good idea to cache the countries ArrayList to avoid the overhead of creating the list every time a drop-down with countries needs to be build.

That's it. When the page with the drop-down is requested in the browser, the new custom item will appear at the top of the list with countries.

ASP.NET 2.0 and Later

In ASP.NET 2.0 and onwards things are much easier. The DropDownList control and other list controls now have a AppendDataBoundItems property. With this property set to true, a call to DataBind leaves all existing items in the list. That allows you to add the extra items declaratively in the markup:

<asp:DropDownList ID="DropDownList1" runat="server" 
            AppendDataBoundItems="true">
<asp:ListItem Value="">Please select a country</asp:ListItem>
</asp:DropDownList>

That's it. No more messing around with code in the code behind to add the item. Just set AppendDataBoundItems to true and the manually added item stays where it was.


Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Friday, March 11, 2005 6:11:51 AM sriniva vaskuri said:
Hi

      I got excellent login from ur website for real projects
On Tuesday, April 19, 2005 8:05:41 AM B.S.van Veen said:
Instead of returning an arraylist the function getcountries can also return a listitemcollection. However I tried this and after the databind the value attribute of the listitem is not present instead the text property took it place.

Any Idead why
On Friday, May 12, 2006 2:39:15 PM sambors said:
I need to fill an array from a database & then add an item to it. It seems to be much more complicated. I am looking for example to copy to my project , cause i got a hang over.
On Friday, May 12, 2006 6:28:39 PM Imar Spaanjaars said:
Hi sambors,

Not sure what you mean / want...

If you have a hangover, it's better to wait until tomorrow and try again....

Imar
On Wednesday, July 18, 2007 3:03:40 PM Ramy Mahmoud said:
Thnxxxxx a lot now I finished my task and I can go Home  on time :) thnx
On Monday, August 06, 2007 6:12:44 AM Rajesh B Patil said:
This post rocks man keep goin
On Saturday, September 01, 2007 5:14:43 PM philip said:
Many Thanks, I'd be lost without resources like this.
On Thursday, October 11, 2007 3:50:46 AM Cam said:
Genius post dude!!

On Wednesday, October 31, 2007 8:21:55 PM John said:
Awesome!  Wish it was this easy for all our probs. lol.
On Thursday, June 12, 2008 10:52:31 AM chandu said:
how to create dropdownlist  to select multiplevalues
On Thursday, June 12, 2008 11:09:53 AM Imar Spaanjaars said:
Hi chandu,

How is this related to the topic of this article?

Imar
On Wednesday, October 08, 2008 8:29:28 AM Dheeraj Singh said:
Thanx,
Very helpful article
On Sunday, October 18, 2009 7:21:24 AM Maheel said:
Great work. Saved a lot of time. Keep up great work.
On Wednesday, November 11, 2009 3:44:41 PM Ujval said:
Very Useful
On Sunday, November 22, 2009 9:52:27 PM Greg said:
AppendDataBoundItems="true"  -  that's a little golden nugget - Thanks for the post!  
On Monday, December 21, 2009 7:03:07 AM Patrick said:
Thanks for explaining the techniques.......!!!
On Tuesday, January 05, 2010 3:39:58 PM Michael said:
If you use this with append, fill the dropdownlist only if the page is not a postback, otherwise you're getting your data in the dropdownlist multiple times.

Regards
On Wednesday, January 27, 2010 7:19:03 PM Scott said:
I can't get it to work using a dynamic DataSourceID assigned through code.  I'm using a checkbox to determine which of two queries to run and use to populate the DDL and want a default valut as the first item in the list.  How is this possible?
On Wednesday, January 27, 2010 8:30:43 PM Imar Spaanjaars said:
Hi Scott,

Hard to say as you don't define "can't get it to work". Can you post this on the Wrox forum (http://p2p.wrox.com/index.php?referrerid=385) and provide more details and the code you're using? If you post the link here, I'll take a look.

Cheers,

Imar
On Sunday, August 22, 2010 5:08:04 PM Roman said:
Thank you! Helped a lot!
On Monday, November 29, 2010 5:58:55 AM Arnold Shavings said:
any possibily the above script in php?  I've been trying to fine one script like the above in php mysql for a while now.
On Monday, November 29, 2010 8:00:56 AM Imar Spaanjaars said:
Hi Arnold,

Don't know. I am not much of a PHP guy. Can't you just echo a new element after the opening select tag?

Imar
On Wednesday, February 16, 2011 8:44:16 AM Vaal Donkie said:
AppendDataBoundItems="true" is a problem if you want to re-populate the dropdownlist with another set of values.

You'll end up with the first list, plus the second list, which is usually not what you want.

What I've come up with is to programmatically insert the item at position 0 in the collection right after calling databind():

DDLDupeArticles.DataTextField = "heading"
DDLDupeArticles.DataValueField = "contentid"
DDLDupeArticles.DataBind()
Dim li As New System.Web.UI.WebControls.ListItem("--Please Select an Article--", "-1")
DDLDupeArticles.Items.Insert(0, li)
On Wednesday, February 16, 2011 8:47:37 AM Imar Spaanjaars said:
Hi Vaal,

Yes, that's the way to do it. You find similar code at the start of the article which was the only way to do it in older versions of .NET.

You could also call control.Items.Clear() but you would still need to add the item manually.

Cheers,

Imar
On Wednesday, July 06, 2011 8:32:57 AM Gideon said:
Hi Imar,

You have stated the value for your appended listitem as "".  Can this value be changed to something that would in your example select all the countries?

Tnx,

Gideon
On Wednesday, July 06, 2011 9:08:13 AM Gideon said:
Got it.

Set value to "-1" and in SQL select statement that use Drop down list add ([Country] = CASE WHEN @Country = -1 THEN [Country] ELSE @Country END).

Currently enjoying Beginning ASP.NET 3.5.  Well and clearly written, tnx!

Gideon
On Wednesday, July 06, 2011 12:43:34 PM Imar Spaanjaars said:
Yes, that works. Or you can simply compare the parameter with -1:

WHERE Country = @Country OR @Country = -1

Hope this helps.

>> Currently enjoying Beginning ASP.NET 3.5.  Well and clearly written, tnx!

Thank you!


Imar
On Monday, August 01, 2011 11:02:03 PM Abhi said:
can you explain, How to bind data from database into dropdownlist, also want to know how to get the selected item value?
On Tuesday, August 02, 2011 1:49:36 AM Imar Spaanjaars said:
Hi Abhi,

There are hundreds of articles on the web that expain how to do this:

http://www.google.com/#sclient=psy&hl=en&source=hp&q=databind+dropdownlist+asp.net

You can get the selected value using any of the Selected* properties of the DropDownList control: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.aspx

Cheers,

Imar
On Tuesday, August 02, 2011 1:52:11 AM Imar Spaanjaars said:
Abhi, if you want to be notified of follow up posts, consider entering a valid e-mail address.....

Imar
On Sunday, November 27, 2011 6:32:51 AM abie said:
Thanx alot
On Sunday, January 29, 2012 5:36:39 PM SUMAN said:
This is awesome.That's why Microsoft is so user friendly and giving such kind of properties and also thanks to you for helping me!! it's really works my dear
On Wednesday, July 25, 2012 4:01:04 AM Diana said:
if this is applied to a second dropdownlist that is pre-filled due to selection in the first dropdownlist, it's not working properly. the databound items from the previous selection (in the first ddl) remain in the itemlist.

anybody know how to solve this?
  
On Wednesday, July 25, 2012 1:16:55 PM Imar said:
Hi Diana,

You can call Clear() on the Items collection.

Imar
On Thursday, September 13, 2012 7:12:21 AM Biswajit PAl said:
it works....
Thank you

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.