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 727549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:34:48+00:00 2026-05-14T06:34:48+00:00

I have a rather large application that has literally a hundred DDLs with Yes

  • 0

I have a rather large application that has literally a hundred DDLs with Yes / No ListItems. In an attempt to save myself some time, I created a custom control that extends the standard DDL.

It all seems to work fine but I am having some issues when assigning the SelectedValue property in code where the selected value does not seem to have an affect on the control. I wonder if I should be adding my items during Init or PagePreLoad? Should I be calling base.OnInit before or after I add the list items? This mostly works but not 100%. (v3.5)

public class YesNoDropDownList : DropDownList
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!Page.IsPostBack)
        {
            base.Items.Add(new ListItem("Yes", "YES"));
            base.Items.Add(new ListItem("No", "NO"));
        }
    }
}

I think the issue is that if I load ListItems in Init, that is before viewstate is established and the ListItems are lost on postback. If I load them in OnLoad, that is after SelectedValue is applied and if I am setting SelectedValue, the selection is lost. My solution was wire up the Page InitComplete event in the OnInit override. This works but I am not sure that it is the best solution.

So, either Page_InitComplete as detailed below or OnInit but I have to load the items every time. Thoughts?

protected override void OnInit(EventArgs e)
{
    this.Page.InitComplete += new EventHandler(Page_InitComplete);

    base.OnInit(e);
}

private void Page_InitComplete(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        base.Items.Add(new ListItem("Yes", "YES"));
        base.Items.Add(new ListItem("No", "NO"));
    }
}
  • 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-14T06:34:49+00:00Added an answer on May 14, 2026 at 6:34 am

    I would suggest adding the items in OnInit instead of on PreLoad, that way your control starts it’s life with the state and whatnot properly set. Relevant bit of documentation (emphasis mine)

    Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
    Use this event to read or initialize control properties.

    ViewState tracking also gets turned on between Init and PreLoad so it would be better if the control had all of it’s options created before.

    The code for SelectedValue is (thank you reflector):

        set
        {
            if (this.Items.Count != 0)
            {
                if ((value == null) || (base.DesignMode && (value.Length == 0)))
                {
                    this.ClearSelection();
                    return;
                }
                ListItem item = this.Items.FindByValue(value);
                if ((((this.Page != null) && this.Page.IsPostBack) && this._stateLoaded) && (item == null))
                {
                    throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedValue" }));
                }
                if (item != null)
                {
                    this.ClearSelection();
                    item.Selected = true;
                }
            }
            this.cachedSelectedValue = value;
        }
    

    If you try to set the SelectedValue before the list of items is populated, it’s basically a no-op.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.