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"
- <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="LearningEvents.WebUserControl1" %>
-
- <asp:Button ID="Button1" runat="server" Text="Child" onclick="Button1_Click" />
Code for "WebUserControl1.ascx.cs"
- using System;
-
- namespace LearningEvents
- {
- public partial class WebUserControl1 : System.Web.UI.UserControl
- {
- public delegate void ButtonClick(object sender, EventArgs e);
-
- public ButtonClick BtnDelegate{get;set;}
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- BtnDelegate.Invoke(null, null);
- }
- }
- }
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"
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LearningEvents.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <%@ Register TagPrefix="uc" TagName="ucChildControl" Src="~/WebUserControl1.ascx" %>
-
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Parent" OnClick="Button1_Click" />
- <uc:ucChildControl ID="ucChildControl1" runat=server />
- <br /><br />
- <asp:Label ID="lblInfo" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
Code for "WebForm1.aspx.cs"
- using System;
-
- namespace LearningEvents
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ucChildControl1.BtnDelegate = new WebUserControl1.ButtonClick(Button1_Click);
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (e == null)
- lblInfo.Text = "I am called From Child";
- else
- lblInfo.Text = "I am called From Parent";
- }
- }
- }
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:
When the child is clicked:
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.