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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:02:11+00:00 2026-05-12T08:02:11+00:00

I have a page and a user control — we’ll call them Detail.aspx and

  • 0

I have a page and a user control — we’ll call them Detail.aspx and Selector.ascx.

Let’s say the page shows the details of individual records in a database. The user control basically consists of a DropDownList control and some associated HTML. The DropDownList displays a list of other records to switch to at any time.

When the DropDownList fires its SelectedIndexChanged event, I’d like the parent page, Detail.aspx in this case, to handle it. After all, he’ll need to know what was selected so that he can appropriately change the URL and the details shown, etc.

To do that, I’ve done what I usually do, which is also what the top answer says to do in this StackOverflow question:

public event EventHandler DropDownSelectedIndexChanged
{
    add
    {
        MyDropDownList.SelectedIndexChanged += value;
    }
    remove
    {
        MyDropDownList.SelectedIndexChanged -= value;
    }
}

The above code appears in the Selector.ascx.cs codebehind file.

As a result, on Detail.aspx, I can use it like so:

<cc1:RecordSelector ID="RecordSelector1" runat="server"
OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />

So far nothing fancy or surprising.

Here is my problem:

This causes a NullReferenceException when the browser hits Detail.aspx.

Debugging the problem shows that when the page is first hit, the public event I’ve shown above tries to add the event, but MyDropDownList is null, thus throwing the exception. From what I can tell, the events are added (or attempted to be added) before the Selector user control’s Load event fires and thus also before the DropDownList’s Load event fires.

Curiously, if I omit the OnDropDownSelectedIndexChanged attribute from Detail.aspx and instead put the following in the Page_Load event in Detail.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    RecordSelector1.DropDownSelectedIndexChanged += new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
}

It works exactly as expected. The events are attached and handled just fine. No problems.

But this means several bad things:

  1. I have to remember not to use the designer to add said event onto my user control
  2. I have to remember not to add the event via attributes when working in source view
  3. Worst of all, as the control’s author I need to make sure everybody else using my control knows 1 and 2

So what am I doing wrong? Every example I’ve seen thus far shows similar usage of exposing child controls’ events through a user control.

  • 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-12T08:02:12+00:00Added an answer on May 12, 2026 at 8:02 am

    The reason this works:

    protected void Page_Load(object sender, EventArgs e)
    {
        RecordSelector1.DropDownSelectedIndexChanged 
            += new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
    }
    

    and this does not:

    <cc1:RecordSelector ID="RecordSelector1" runat="server"
    OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />
    

    is because the first one adds the handler after the control has been initialized (via the page’s Init). The second example gets parsed much earlier and as such the page is attempting to add the handler before the control has initialized.

    Due to the nature of the page’s life cycle I think you may have to live with adding the event handler in the code-behind. There will be no way to add the handler before the control is initialized because that control will always be null prior to initialization.

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

Sidebar

Related Questions

my structure is like this: master page aspx(web form) ascx(user control) I have a
I have a User Control called: Share.ascx I have a page called: News.aspx In
I have aspx page with a user control. I want to implement caching for
I have an aspx page that contains a user control. The user control has
i have default.aspx page and one user control. usercontrol is having following code for
I have say this hierarchy in ASP.NET: page user control 1 user control 2
I have a page (A) and a user control and I want to send
I have a page in which I am loading a user control dynamically as
I have a drop-down list hardcoded in an MVC View User Control page and
Is there a way to access page controls from user control . I have

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.