Fire Event From Child To Parent in C#

Recently, I came across the situation where a child user control needed to fire an event on the parent control.

I resolved the problem using delegates. Let's see how the problem was resolved considering a small example.

For simplicity I have designed a Webform containing a button, label and user control.

This user control contains the one button that fires an event on the parent control in which we set the text for the label containing the name of the button from which the event was fired.

Implementation


Step 1

Create an Empty Web Application and name it "LearningEvents".

Step 2

Add a User Control "WebUserControl1.ascx" and write the HTML and code as in the following.

HTML for "WebUserControl1.ascx"

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="LearningEvents.WebUserControl1" %>  
  2.   
  3. <asp:Button ID="Button1" runat="server" Text="Child" onclick="Button1_Click" />  
Code for "WebUserControl1.ascx.cs"
  1. using System;  
  2.   
  3. namespace LearningEvents  
  4. {  
  5.     public partial class WebUserControl1 : System.Web.UI.UserControl  
  6.     {  
  7.         public delegate void ButtonClick(object sender, EventArgs e);  
  8.   
  9.         public ButtonClick BtnDelegate{get;set;}  
  10.   
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.   
  14.         }  
  15.   
  16.         protected void Button1_Click(object sender, EventArgs e)  
  17.         {  
  18.             BtnDelegate.Invoke(nullnull);  
  19.         }  
  20.     }  
  21. }  

Please note that in the preceding code we have declared a public delegate "ButtonClick" and public property "BtnDelegate" of type ButtonClick.

To fire an event on the parent control we will invoke BtnDelegate.

Step 3

Add a User Control "WebForm1.aspx" and write the HTML and code as in the following code snippet.

HTML for "WebForm1.aspx"

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LearningEvents.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <%@ Register TagPrefix="uc" TagName="ucChildControl" Src="~/WebUserControl1.ascx" %>  
  4.   
  5.   
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head runat="server">  
  8.     <title></title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.     <div>  
  13.         <asp:Button ID="Button1" runat="server" Text="Parent" OnClick="Button1_Click" />  
  14.         <uc:ucChildControl ID="ucChildControl1" runat=server />  
  15.         <br /><br />  
  16.         <asp:Label ID="lblInfo" runat="server"></asp:Label>  
  17.     </div>  
  18.     </form>  
  19. </body>  
  20. </html>  
Code for "WebForm1.aspx.cs"
  1. using System;  
  2.   
  3. namespace LearningEvents  
  4. {  
  5.     public partial class WebForm1 : System.Web.UI.Page  
  6.     {  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.             ucChildControl1.BtnDelegate = new WebUserControl1.ButtonClick(Button1_Click);  
  10.         }  
  11.   
  12.         protected void Button1_Click(object sender, EventArgs e)  
  13.         {  
  14.             if (e == null)  
  15.                 lblInfo.Text = "I am called From Child";  
  16.             else  
  17.                 lblInfo.Text = "I am called From Parent";  
  18.         }  
  19.     }  
  20. }  
Please note that in the preceding code we have pointed ucChildControl1.BtnDelegate towards the Button1_Click event in the page load.

Run the page and the following output will be observed.

When the parent is clicked:

run
When the child is clicked:

output

I hope this sample will help in situations where events from a child needs to inform the parent of the completion of the job.

Happy Coding.