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

  • Home
  • SEARCH
  • 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 80791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:21:29+00:00 2026-05-10T21:21:29+00:00

How do I raise an event from a user control that was created dynamically?

  • 0

How do I raise an event from a user control that was created dynamically?

Here’s the code that I’m trying where Bind is a public EventHandler

protected indDemographics IndDemographics; protected UserControl uc; override protected void OnInit(EventArgs e) {     uc = (UserControl)LoadControl('indDemographics.ascx');     IndDemographics.Bind += new EventHandler(test_handler);     base.OnInit(e); } 

I get a null object for IndDemographics. Can anyone point me to a complete code sample? Thanks in advance…

  • 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. 2026-05-10T21:21:30+00:00Added an answer on May 10, 2026 at 9:21 pm

    First off, you’ll need to make sure that you have the event defined in your usercontrol’s code.

    for example:

    public class MyUserControl   Inherits UserControl    Public Event Bind(sender as object, e as EventArgs)    public sub SomeFunction()      RaiseEvent Bind(me, new EventArgS())   End Sub End Class 

    After this, then you can bind to the Event. Now,for your other issue, are you loading this control dynamically or is it declared on your ASPX side? If it’s on your ASPX side, then you don’t need the LoadControl, as declaring an object as Runat=Server on the ASPX side instantiates an instance of said class.

    If not, then you’ll need to make sure you’re using the Virtual Path for the location of the ASCX file. (in your example, you’d use ‘~/indDemographics.ascx’ if the ASCX was at the root of the website). At this point you’d need to add it to the page (or a placeholder or some other container object).

    Regardless, of which way you instantiate an instance of the UserControl, you then associate the Event Handler to the Event of the instance of the class. For example:

    Dim btn As New Button; AddHandler btn.Click, AddressOf MyButtonClickEventHandler 

    Now, for the reason that you’re getting a NULL reference in the example code.

    When you use the LoadControl reference, then the instance of your object is in the UC variable. In the example, you declare two objects, UC as a type of UserControl and indDemographics as a type of indDemographics.

    When you use the LoadControl, you’re instantiating an instance of indDemographics and assigning it to UC. When you try to assign the event handler to the IndDemographics variable, it has never actually been instantiated.

    Ultimately, your code should look more along these lines:

    protected indDemographics IndDemographics; override protected void OnInit(EventArgs e) {     indDemographics = LoadControl('~/indDemographics.ascx');     IndDemographics.Bind += new EventHandler(test_handler);     base.OnInit(e); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer var timeoutId = -1; function functionToRun() { alert('hi there!'); }… May 11, 2026 at 2:07 pm
  • added an answer You can dynamically create and manipulate stylesheets. See here for… May 11, 2026 at 2:07 pm
  • added an answer Does FooClass have the XmlRootElement annotation? If not, try: Source… May 11, 2026 at 2:07 pm

Related Questions

How do I make the most out of a MS in Business Analytics?
How do I tell my new employer that I can’t use the computer they gave me?
How do I store binary data in MySQL ?
How do I generate an ETag HTTP header for a resource file?
I'm writing my first app with ASP.NET MVP (attempting Supervisory Controller) and Unit Testing
I can get an object if it's moused over. I can get the coordinates
I have a third-party editor that basically comprises a textbox and a button (the
How do you get two unrelated controls to raise the same custom event? All

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.