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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:18:12+00:00 2026-05-10T15:18:12+00:00

What is best practises for communicating events from a usercontrol to parent control/page i

  • 0

What is best practises for communicating events from a usercontrol to parent control/page i want to do something similar to this:

MyPage.aspx: <asp:Content ID='Content1' ContentPlaceHolderID='MainContentPlaceholder' runat='server'>   <uc1:MyUserControl ID='MyUserControl1' runat='server'       OnSomeEvent='MyUserControl_OnSomeEvent' />  MyUserControl.ascx.cs: public partial class MyUserControl: UserControl {     public event EventHandler SomeEvent; ....    private void OnSomething()     {         if (SomeEvent!= null)             SomeEvent(this, EventArgs.Empty);     } 

Question is what is best practise?

  • 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-10T15:18:13+00:00Added an answer on May 10, 2026 at 3:18 pm

    You would want to create an event on the control that is subscribed to in the parent. See OdeToCode for an example.

    Here is the article for longevity sake:

    Some user controls are entirely self contained, for example, a user control displaying current stock quotes does not need to interact with any other content on the page. Other user controls will contain buttons to post back. Although it is possible to subscribe to the button click event from the containing page, doing so would break some of the object oriented rules of encapsulation. A better idea is to publish an event in the user control to allow any interested parties to handle the event.

    This technique is commonly referred to as “event bubbling” since the event can continue to pass through layers, starting at the bottom (the user control) and perhaps reaching the top level (the page) like a bubble moving up a champagne glass.

    For starters, let’s create a user control with a button attached.

    <%@ Control Language='c#' AutoEventWireup='false'      Codebehind='WebUserControl1.ascx.cs'      Inherits='aspnet.eventbubble.WebUserControl1'      TargetSchema='http://schemas.microsoft.com/intellisense/ie5' %> <asp:Panel id='Panel1' runat='server' Width='128px' Height='96px'>     WebUserControl1      <asp:Button id='Button1' Text='Button' runat='server'/> </asp:Panel> 

    The code behind for the user control looks like the following.

    public class WebUserControl1 : System.Web.UI.UserControl {    protected System.Web.UI.WebControls.Button Button1;    protected System.Web.UI.WebControls.Panel Panel1;     private void Page_Load(object sender, System.EventArgs e)    {       Response.Write('WebUserControl1 :: Page_Load <BR>');    }     private void Button1_Click(object sender, System.EventArgs e)    {       Response.Write('WebUserControl1 :: Begin Button1_Click <BR>');       OnBubbleClick(e);       Response.Write('WebUserControl1 :: End Button1_Click <BR>');    }     public event EventHandler BubbleClick;     protected void OnBubbleClick(EventArgs e)    {       if(BubbleClick != null)       {          BubbleClick(this, e);       }    }                #region Web Form Designer generated code    override protected void OnInit(EventArgs e)    {       InitializeComponent();       base.OnInit(e);    }     private void InitializeComponent()    {       this.Button1.Click += new System.EventHandler(this.Button1_Click);       this.Load += new System.EventHandler(this.Page_Load);     }    #endregion  } 

    The user control specifies a public event (BubbleClick) which declares a delegate. Anyone interested in the BubbleClick event can add an EventHandler method to execute when the event fires – just like the user control adds an EventHandler for when the Button fires the Click event.

    In the OnBubbleClick event, we first check to see if anyone has attached to the event (BubbleClick != null), we can then invoke all the event handling methods by calling BubbleClick, passing through the EventArgs parameter and setting the user control (this) as the event sender. Notice we are also using Response.Write to follow the flow of execution.

    An ASPX page can now put the user control to work.

    <%@ Register TagPrefix='ksa'      TagName='BubbleControl'      Src='WebUserControl1.ascx'  %> <%@ Page language='c#' Codebehind='WebForm1.aspx.cs'      AutoEventWireup='false' Inherits='aspnet.eventbubble.WebForm1'  %> <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN' > <HTML>     <HEAD>         <title>WebForm1</title>     </HEAD>     <body MS_POSITIONING='GridLayout'>         <form id='Form1' method='post' runat='server'>             <ksa:BubbleControl id='BubbleControl' runat='server' />         </form>     </body> </HTML> 

    In the code behind for the page.

    public class WebForm1 : System.Web.UI.Page {    protected WebUserControl1 BubbleControl;     private void Page_Load(object sender, System.EventArgs e)    {       Response.Write('WebForm1 :: Page_Load <BR>');    }     #region Web Form Designer generated code    override protected void OnInit(EventArgs e)    {       InitializeComponent();       base.OnInit(e);    }     private void InitializeComponent()    {           this.Load += new System.EventHandler(this.Page_Load);       BubbleControl.BubbleClick += new EventHandler(WebForm1_BubbleClick);    }    #endregion     private void WebForm1_BubbleClick(object sender, EventArgs e)    {       Response.Write('WebForm1 :: WebForm1_BubbleClick from ' +                       sender.GetType().ToString() + '<BR>');             } } 

    Notice the parent page simply needs to add an event handler during InitializeComponent method. When we receive the event we will again use Reponse.Write to follow the flow of execution.

    One word of warning: if at anytime events mysteriously stop work, check the InitializeComponent method to make sure the designer has not removed any of the code adding event handlers.

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

Sidebar

Related Questions

Wondering what others do / best practice for communicating between layers. This question relates
I am reading through titanium best practises and i was wondering why this doesnt
What are the best practices for creating exceptions? I just saw this, and I
This is not an homework. But an practice to understand what are the best
What are the best practises to update/move a Database in Prod with the dev
Well, I didn't follow the best practises and I'm now kind of stuck, not
I am interested in the best practices to access Windows Azure API from a
Are there best practises how to improve the performance of spring webapps? I use
Are there best practices for sending data to/from iOS? Should I stay away from
Best practices or tools for installing a SQL Server database I have a SQL

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.