FindByValue in ASP.NET DropDownList

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();
}
+3
4

, , DataBind . , FindByValue()?

- ?

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();
        }
    }
}
+6

,

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();
        }
    }
}
0

:

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;
 }
}
}
0

_Load :

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       ddlAggerationUnitId.DataSource = ExternalAccount.GetAggregationUnits();
       ddlAggerationUnitId.DataTextField = "Value";
       ddlAggerationUnitId.DataValueField = "Key";
       ddlAggerationUnitId.DataBind();
    }
}
0

Source: https://habr.com/ru/post/1777733/


All Articles