HOME
ABOUT ME
Biography
How I Became Boog
The Real Boog Powell
My Resume
WRITING
Stories
Reviews For Writers
Writing Links
CONTACT
   
   

Visit my other site:
Bleeker Books

Friends Of Boog


Recent Posts


Complete Archives

"Ah, but I was so much older then, I'm younger than that now."

My Boog Pages


Monday, July 10
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.

Tags: , , .


posted by Graham at 2:26 PM permalink

Trackbacks | Comments (2)


This page is powered by Blogger. Isn't yours?

Listed on BlogShares