Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with null values in a drop down list data bind

Status
Not open for further replies.

raindogs

Programmer
Nov 23, 2005
27
US
I have a drop down list where the "SelectedValue" attribute is bound to a field that may contain nulls. In the cases where it is null, I get an error when the page loads. To fix this, I'd like to set something up where, if the value is null, a new item is added to the drop down list that reads "-- Make a Selection --" and this new value is selected. If it's not null, the page should skip all this and proceed normally. I've got everything working except for the fact that it adds and selects "-- Make a Selection --" every time, even when there should be a value.

Here's the code for the drop down list:

<asp:DropDownList ID="ddlUnitZone" runat="server" DataSourceID="zones_list_source" OnDataBinding="ddlUnitZone_OnDataBinding"DataTextField="zone_name" DataValueField="unit_zone" SelectedValue='<%# Bind("unit_zoneid")%>'>
</asp:DropDownList>

and here's the method that is called on data binding:

protected void ddlUnitZone_OnDataBinding(object sender, EventArgs e)
{
DropDownList zoneList = (DropDownList)sender;
if (zoneList.SelectedValue == null || zoneList.SelectedValue == "")
{
zoneList.Items.Add("-- Make a Selection --");
zoneList.AppendDataBoundItems = true;
zoneList.SelectedValue = "-- Make a Selection --";
}
}

Clearly, the selected value is not making its way into my method and the method is seeing null every time. Can anyone see how to fix this?

Thanks a lot,
Alex
 
hi,

why not fix this problem at the query level? are you using SQL Server? in that case try the isnull() function...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top