Nov 20 2008
ASP.NET DropDownList and null value (or nullable database field)
If you have an ASP.NET detailsview or similar control, you may already have added extended control, such as radiobuttonlists or dropdowns. If so, you may have experienced and error when you use such controls binded to a database field which is nullable, and has a null value. In this case, the bind fails. To avoid this error, simply add a list item to the collection of the dropdownlist, like this:
<asp:ListItem Selected=”True” Value=”">Nessuna</asp:ListItem>
Add the AppendDataBoundItems property to the dropdownlist:
asp:DropDownList ID=”ddl” runat=”server” DataSourceID=”mydatasource”
DataTextField=”text” DataValueField=”id”
SelectedValue=’<%# Bind(“id”) %>’ AppendDataBoundItems=”true” >
et voila, it works! The trick is using Value=”", which is interpreted as a null value during databinding.
Happy coding!


Default
Small Fonts
Big Fonts