Wednesday, October 25, 2006

Handling null values when binding to ASP.NET 2.0 DropDownList Controls

Frequently when using 2 way data binding on data controls such as the FormView or DetailsView, DropDownList boxes are used to allow users to select pre-populated values.

Often in this situation the data item the DropDownList control is being bound to contains a null value. In these situations the ASP.NET compiler will throw an exception (SelectedValue property is set to a value not in its collection of list items).

To solve this issue it is necessary to add in a ListItem with an empty value to the DropDownList. To do this add the following line to your DropDownList making sure you also set the property of the AppendDataBoundItems to "true" so that it will be appended to the list:

<asp:dropdownlist AppendDataBoundItems="true">
<asp:listitem value="" text="Select a value"></asp:listitem>
</asp:dropdownlist>


To make your application more userfriendly it would be nice to programmatically show the null value ListItem when your DropDownList has not been bound to a value. To do this we add an
OnDataBound event to the DropDownList control and check to see if it has a selected value. If it doesn't, then we want to show the null value ListItem. Make sure you set the value of Enabled to false on the ListItem so it doesn't appear.

Add the OnBoundEvent to the control:
<asp:dropdownlist id="MyDropDownList" runat="server" OnDataBound="AddNullValueToDropDownList_DataBound" AppendDataBoundItems="true">
<asp:listitem value="" text="Select a value" Enabled="false"></asp:listitem>
</asp:dropdownlist>

Create the event in the code behind:
protected void AddNullValueToDropDownList_DataBound(object sender, EventArgs e)
{
DropDownList dropdownlist = (DropDownList)sender;
if (dropdownlist.SelectedValue == string.Empty)
{
dropdownlist.Items[0].Enabled = true;
}
}

That's it! Now you can easily databind a DropDownList to your favourite data control with null values.

5 Comments:

At 3:51 PM, Blogger snydez said...

thx,
so many people suggesting
<asp:listitem value="0" text="Select a value"></asp:listitem>

which is failed to grab the null :(

 
At 10:42 AM, Blogger Unknown said...

Francesco,

The post was just wanted i needed to get started. I added one little twist to it, and used it on two different DropDownLists. Basically, I used your method to handle the null value. Then, based on the DropDownList used, I selected what I wanted as the default. Finally, I disabled the Select Value item again.

Here is the code:

protected void AddNullValueToDropDownList_DataBound(object sender, EventArgs e)
{
DropDownList dropdownlist = (DropDownList)sender;

if (dropdownlist.SelectedValue == string.Empty)
{
dropdownlist.Items[0].Enabled = true;

if (dropdownlist.ID == "ddl1")
{
dropdownlist.SelectedValue = "None";
}
if (dropdownlist.ID == "ddl2")
{
dropdownlist.SelectedValue = "Unknown";
}

dropdownlist.Items[0].Enabled = false;
}
}

 
At 11:58 PM, Blogger Smarter Online said...

HI..

 
At 4:57 AM, Blogger matt_rummler said...

This is interesting. I am using .NET 4 in Visual Studio 2010 and I still receive this error using the dropdownlist control.

Also interesting, while your solution seemed one of the most sound & complete solutions to this problem it still has not resolved the issue for me.

The fact that Microsoft has not addressed this obvious bug (and yes it could be stated that all of us who have had the issue simple don't understand how to use the control... which I think is laughable).

More of a rant than anything I will be using exception handling to prevent this lame bug from causing my entire form not to work... and the funny thing is that there really isn't anything to be done... having a null or empty value in a dropdown in edit mode is STINKIN FINE!!! The user will have the option of entering the value themselves and, in fact, the field is required on the form...

 
At 6:34 AM, Blogger Unknown said...

Your solution worked great!! I was about to restructure my database key field solve this error, and you have me a much easier and better way.

 

Post a Comment

<< Home