I have the following code in a user control that contains a DropDownList named ddlAggerationUnitId. DropDownList is a DataBind'd in the Page_Load () event. The "value" is set to 40, and it exists. If I remove the logic for the installed method, the page will load and select the correct element, but if the value is fake, the page throws an exception. I would like to avoid this exception by seeing if there is a value before trying to establish it, so why do I need logic.
Now it seems that the compiler evaluates the if statement as false, although I know this 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! Thank!
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();
}