Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:24:56+00:00 2026-05-18T08:24:56+00:00

I have the following code in a custom user control that contains a DropDownList

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:24:56+00:00Added an answer on May 18, 2026 at 8:24 am

    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();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom user control that contains asp:ValidationSummary . It is placed on
I have the following code that processes an INotification (custom interface) var mgr =
I have the following simple custom control that I have defined for a Windows
I have a custom user control DatePicker.cs. Inside of another piece of code I
I have a custom WPF user control called a TimeoutPanel that I am trying
I have a simple custom user control that uses jqGrid. the control is as
I have the following XAML code as part of a custom control: <telerik:RadTreeView x:Name=treeModules>
I have following code using hibernate to throw a custom exception on error and
I have the following code for my custom Constraint: @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD
I have the following code to show me a custom list with images and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.