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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:05:37+00:00 2026-05-23T20:05:37+00:00

I am having a very hard time subscribing to events in a dynamically loaded

  • 0

I am having a very hard time subscribing to events in a dynamically loaded user control. It’s probably easiest to just look at the code I am having trouble with. Any help is greatly appreciated.

On the parent page I use this method to dynamically load user controls.

 private void LoadMyUserControl(string controlName)
 {
      parent.Controls.Clear();

      // Load the user Control
      UserControl MyControl = (UserControl)LoadControl(ControlName);

      string userControlID = controlName.Split('.')[0];
      MyControl.ID = userControlID.Replace("/","").Replace("~", "");
      Type ucType = MyControl.GetType();

      // Set misc parameters
      PropertyInfo myProp = ucType.GetProperty("MyProp");
      myProp.SetValue(MyControl, "MyVal", null);

      //Dynamically subscribe to user control event
      Type objectType = MyControl.GetType();
      EventInfo objectEventInfo = objectType.GetEvent("updateParent");
      Type objectEventHandlerType = objectEventInfo.EventHandlerType;            
      MethodInfo mi = this.GetType().GetMethod("HandledEvent");
      Delegate del = Delegate.CreateDelegate(objectEventHandlerType, this, mi);
      objectEventInfo.AddEventHandler(this, del);

  }

  public void HandledEvent (Object sender, EventArgs e)
  {

  }

My user control has a simple public event like this

  public partial class MyUserControl1 : System.Web.UI.UserControl
  {
         public int MyProp { get; set; }
         public event EventHandler updateParent;
  }

The error I am getting is on the ObjectEventInfo.AddEventHandler line stating “Object does not match target type.”

Thanks again in advance for your assistance.

  • 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-23T20:05:38+00:00Added an answer on May 23, 2026 at 8:05 pm

    Solution #1: Create a common interface.

    // IParentControlUpdater.cs
    namespace AspNetUserControlDynamicEvent {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
    
        interface IParentControlUpdater {
            event EventHandler<EventArgs> UpdateParent;
        }
    }
    
    // DynamicUserControl.ascx.cs
    namespace AspNetUserControlDynamicEvent {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
    
        public partial class DynamicUserControl : System.Web.UI.UserControl, IParentControlUpdater {
            protected void Page_Load(object sender, EventArgs e) { }
    
            #region IParentControlUpdater Members
    
            public event EventHandler<EventArgs> UpdateParent;
    
            #endregion
        }
    }
    
    // Default.aspx.cs
    namespace AspNetUserControlDynamicEvent {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
    
        public partial class _Default : System.Web.UI.Page {
            protected void Page_Load(object sender, EventArgs e) { }
    
            private void LoadMyUserControl(string controlName) {
                UserControl control = (UserControl)LoadControl(controlName);
    
                // Do some stuff 
    
                ((IParentControlUpdater)control).UpdateParent += new EventHandler<EventArgs>(control_UpdateParent);
            }
    
            private void control_UpdateParent(object sender, EventArgs e) {
                throw new NotImplementedException();
            }
        }
    }
    

    Solution #2: Create a base class.
    Create a base user control class (ie. DynamicUserControlBase for example).

    // DynamicUserControlBase.ascx.cs
    namespace AspNetUserControlDynamicEvent {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
    
        public partial class DynamicUserControlBase : System.Web.UI.UserControl {
            protected void Page_Load(object sender, EventArgs e) { }
    
            // Base event
            public event System.EventHandler<System.EventArgs> UpdateParent;
        }
    }
    

    Then in your user control’s you want to dynamically create and subscribe the updateParent event:

    // DynamicUserControl.ascx.cs
    namespace AspNetUserControlDynamicEvent {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
    
        public partial class DynamicUserControl : DynamicUserControlBase {
            protected void Page_Load(object sender, EventArgs e) { }
        }
    }
    

    Note how it inherits from DynamicUserControlBase. Now, for your LoadMyUserControl method:

    private void LoadMyUserControl(string controlName) {
        DynamicUserControlBase control = (DynamicUserControlBase)LoadControl(controlName);
    
        // Do some stuff     
    
        control.UpdateParent += new EventHandler<EventArgs>(HandledEvent);
    }
    
    private void HandledEvent(object sender, EventArgs e) {
        throw new NotImplementedException();
    }
    

    However, if you are only working with one type of control you could just as easily change the boxing on LoadControl to:

    WebUserControl1 control = (WebUserControl1)LoadControl(controlName); // example
    

    But it seems as though you are possibly looking at loading many different types of user controls, so the first solution should work fine.

    As for your concern via Reflection, try this:

    Type type = MyControl.GetType();
    EventInfo eventInfo = type.GetEvent("UpdateParent");
    EventHandler handler = new EventHandler(HandledEvent);
    eventInfo.AddEventHandler(MyControl, handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably very easy, but I'm having a hard time figuring it out.
I am having a very hard time using the foreach control flow binding in
I am having a very hard time finding a standard pattern / best practice
I've been having a very hard time finding good examples of UIScrollView. Even Apple's
i am very new to Adobe Air and i am having a hard time
I'm having a very hard time getting the 2 buttons in the 2nd child
I am having a very hard time finding resources that talk about the windows
I'm having a very hard time wrapping my head around this problem (it might
I'm having a very hard time trying to do something very simple. Here's the
I'm having a very hard time dealing with multipart/form-data requests with my java application

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.