Blog Indiana 2008

ASP.NET - Adding a blank value to a data-bound dropdownlist

For some reason I can’t seem to remember this when I need it.

If you have a dropdownlist in ASP.NET:

<asp:dropdownlist id="states" runat="server">

If this dropdownlist is databound:

DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("states.xml"));
states.DataSource = ds;
states.DataBind();

…and you want to add a blank value to ensure the user has selected a value, do the following:

states.Items.Insert(0,"");

You can substitute any value for the blank string, such as “–select–”.

Posted on February 23, 2006 in .NET.    

No Comments

(Comments are moderated.)