ASP.NET: Adding a Null Row to a Data Bound Drop-Down List
Today’s .NET tip is a quickie (previous entries here and here): When you are using a database to fill the rows of a data bound drop-down list control, you will inevitably run into an error when the bound field is null or an empty string. You don’t want to add a null row to the list source, so what do you do?
This one’s simple - there’s a property of drop-down lists called AppendDataBoundItems. Set this to true, and you can provide one or more static entries, with rows from the list data source appened beneath them. Sample code:
<asp:DropDownList ID="BoundList"
runat="server"
DataSourceID="ListDataSource"
DataTextField="EntryDescription"
DataValueField="EntryID"
SelectedValue='<%# Bind("BoundItemID") %>'
AppendDataBoundItems="true">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
So, what we have here is a drop-down list called “BoundList”. This list gets its rows from a datasource called “ListDataSource”, displaying the value of “EntryDescription” while binding “EntryID” to the underlying field - which is called “BoundItemID”.
We then provide an empty list item to handle cases where no entry has been selected.
As always, please drop a note in the comments if this code helps you out.








Comment by Anonymous
Cheers. Exactly what I was looking for.
Thanks again
Comment by Anonymous
Thanks man! I was really lost…
Comment by Anonymous
Exactly what I needed! Thanks for the post.
Comment by Anonymous
Thanks, very helpful
Comment by rupa
Hey it works, Thanks for posting .
Comment by Ömür
Thank you very much. That’s what i looked.
That’s realy helpful.
Comment by JB
Thanks, this works nicely.
Just remember that it is not enough to set {Text = “”, Value = “”} in DropDpwnList.Items editor as that will produce an item with no value; you’ll apparently need to add Value=”" manually to the page source.
Comment by i have 4 dropdowns if i select 1& 3 dropdown then only enter into loop otherwise execute outerloop
very good. i am satisfyed
Comment by bill
this helped a lot. I was going crazy trying to figure this out.
Comment by milton
Perfect. Concise and to the point. Thank you very much for posting this!
Comment by Ashu
Thank you so much. I spent a whole hour trying to make this work, but now realized that I was missing AppendDataBoundItems=”true” line.
Comment by Zankar
Thanks. It’s a Good Clue.
Comment by kral oyun
thanks