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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:11:35+00:00 2026-06-11T16:11:35+00:00

I am trying to handle a button click event of a dynamically loaded usercontrol

  • 0

I am trying to handle a button click event of a dynamically loaded usercontrol from my host page. My relevant code is posted below, I think I’m on the right path but what else do I need to make this function properly? I am currently receiveing “Error binding to target method.” when I try to create the usercontrol. Thanks in advance for any assistance!

aspx

<asp:UpdatePanel ID="upLeadComm" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:PlaceHolder ID="phComm" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

aspx.cs

else if (e.CommandName == "GetComm")
{
    string[] cplArg = e.CommandArgument.ToString().Split('§');

    UserControl ucLeadComm = (UserControl)LoadControl("Controls/Comments.ascx");

    // Set the Usercontrol Type 
    Type ucType = ucLeadComm.GetType();

    // Get access to the property 
    PropertyInfo ucPropLeadID = ucType.GetProperty("LeadID");
    PropertyInfo ucPropLeadType = ucType.GetProperty("LeadType");

    EventInfo ucEventInfo = ucType.GetEvent("BtnCommClick");
    MethodInfo ucMethInfo = ucType.GetMethod("btnComm_Click");
    Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType, ucMethInfo);
    ucEventInfo.AddEventHandler(ucType, handler);

    // Set the property 
    ucPropLeadID.SetValue(ucLeadComm, Convert.ToInt32(cplArg[0]), null);
    ucPropLeadType.SetValue(ucLeadComm, cplArg[1], null);

    phComm.Controls.Add(ucLeadComm);

   upLeadComm.Update();
}

ascx.cs

public int LeadID { get; set; }
public string LeadType { get; set; }
public event EventHandler BtnCommClick;

public void btnComm_Click(object sender, EventArgs e)
{
    BtnCommClick(sender, e);
}
  • 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-06-11T16:11:36+00:00Added an answer on June 11, 2026 at 4:11 pm

    I am receiving the error from this line: Delegate handler =
    Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType,
    ucMethInfo);

    The problem is that your passing ucType while you should pass an instance of your UserControl, so try to do:

    Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucLeadComm, ucMethInfo);
    

    I’m not sure that ucLeadComm is an istance of the UserControl because I’ve never used LoadControl(), so if it isn’t use: Activator.CreateInstance(); or use GetContructor() and Invoke() it to create an instance of your object.

    EDIT 1:

    Thanks for the responses, I now receive “Object does not match target
    type.” on the next line: ucEventInfo.AddEventHandler(ucType, handler);

    Also in that line you should pass an instance of your UserControl instead of ucType.

    EDIT 2:

    Many thanks for the assistance! The project builds and does not throw
    any errors. However, how do I go about tying this back into a method
    within the aspx page to actually do something when the button is
    clicked?

    If I understood in this case you should create the method in your aspx.cs:

    public void btnComm_Click(object sender, EventArgs e)
    {
       //Here what you want to do in the aspx.cs
    }
    

    And then create another handler creating a MethodInfo tied to btnComm_Click contained in the aspx.cs and passing it to Delegate.CreateDelegate():

    MethodInfo ucMethInfo = this.GetType().GetMethod("btnComm_Click");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to raise a click event from User control and handle it
I am trying to handle the click of custom button in a jqgrid. I
Here is what I'm trying to do: Click a button on my page, which
Problem 1 I'm trying to use an Onclick event from a search button, that
I am trying to call a shared global event handler from my form's button
I've been trying to handle the onkeydown event across multiple frames (no, I unfortunately
I have a button column on a DataGridView and I am trying to handle
I am trying to add a click event on the element that is return
I'm trying to determine the host operating system's settings regarding mouse button switch state
I'm trying to create Command Buttons dynamically, but clicking the button in question doesn't

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.