FindByValue on ASP.NET DropDownList

狂风中的少年 提交于 2019-12-07 06:58:16

问题


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

回答1:


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



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/4316998/findbyvalue-on-asp-net-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!