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:
<aspropDownList ID="ddlUnitZone" runat="server" DataSourceID="zones_list_source" OnDataBinding="ddlUnitZone_OnDataBinding"DataTextField="zone_name" DataValueField="unit_zone" SelectedValue='<%# Bind("unit_zoneid")%>'>
</aspropDownList>
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
Here's the code for the drop down list:
<aspropDownList ID="ddlUnitZone" runat="server" DataSourceID="zones_list_source" OnDataBinding="ddlUnitZone_OnDataBinding"DataTextField="zone_name" DataValueField="unit_zone" SelectedValue='<%# Bind("unit_zoneid")%>'>
</aspropDownList>
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