I have the following code in a custom user control that contains a DropDownList named ddlAggerationUnitId. The DropDownList is DataBind'd on the Page_Load() event. The "value" is set to be 40 and it DOES exist. If I remove the logic for the set method the page will load and select the correct item, but if the value is bogus the page throws an exception. I'd like to avoid that exception by seeing if the value exists BEFORE trying to set it, hence why the logic is necessary.
Right now it looks like the compiler is evaluating the if statement as false, even though I know for a fact it should be true.
public long? Value
{
get { return Int64.Parse(ddlAggerationUnitId.SelectedItem.Value); }
set
{
if (ddlAggerationUnitId.Items.FindByValue(value.ToString()) != null)
{
ddlAggerationUnitId.SelectedValue = value.ToString();
}
}
}
Any help would be greatly appreciated! Thanks!
EDIT: Here is my Page_Load() event:
protected void Page_Load(object sender, EventArgs e)
{
ddlAggerationUnitId.DataSource = ExternalAccount.GetAggregationUnits();
ddlAggerationUnitId.DataTextField = "Value";
ddlAggerationUnitId.DataValueField = "Key";
ddlAggerationUnitId.DataBind();
}
The following code currently works, however I think it's a bit strange to DataBind twice. This confirms my earlier suspicion that the data was being binded AFTER FindByValue()?
Anyone have any ideas on how to clean this code up?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDdlAggerationUnitId();
}
}
private void BindDdlAggerationUnitId()
{
ddlAggerationUnitId.DataSource = SIGOpsGUI.App_Code.Business.ExternalAccount.GetAggregationUnits();
ddlAggerationUnitId.DataTextField = "Value";
ddlAggerationUnitId.DataValueField = "Key";
ddlAggerationUnitId.DataBind();
}
public long? Value
{
get { return Int64.Parse(ddlAggerationUnitId.SelectedItem.Value); }
set
{
BindDdlAggerationUnitId();
ddlAggerationUnitId.SelectedIndex = -1;
ListItem item = ddlAggerationUnitId.Items.FindByValue(value.ToString());
if (item != null)
{
ddlAggerationUnitId.SelectedValue = value.ToString();
}
}
}
see if following code helps you
updated page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDdlAggerationUnitId();
}
}
private void BindDdlAggerationUnitId()
{
ddlAggerationUnitId.DataSource = ExternalAccount.GetAggregationUnits();
ddlAggerationUnitId.DataTextField = "Value";
ddlAggerationUnitId.DataValueField = "Key";
ddlAggerationUnitId.DataBind();
}
public long? Value
{
get { return Int64.Parse(ddlAggerationUnitId.SelectedItem.Value); }
set
{
ListItem item = null;
if (value.HasValue && ddlAggerationUnitId.Items.Count > 0 && ddlAggerationUnitId.SelectedIndex > 1)
item = ddlAggerationUnitId.Items.FindByValue(value.ToString());
if ( item != null)
{
ddlAggerationUnitId.SelectedValue = value.ToString();
}
}
}
Page_Load should be:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlAggerationUnitId.DataSource = ExternalAccount.GetAggregationUnits();
ddlAggerationUnitId.DataTextField = "Value";
ddlAggerationUnitId.DataValueField = "Key";
ddlAggerationUnitId.DataBind();
}
}
Just a work around:
public long? Value
{
get { return Int64.Parse(ddlAggerationUnitId.SelectedItem.Value); }
set
{
try
{
if (ddlAggerationUnitId.Items.FindByValue(value.ToString()) != null)
{
ddlAggerationUnitId.SelectedValue = value.ToString();
}
}
catch
{
ddlAggerationUnitId.SelectedIndex = -1;
}
}
}