Event Delegation ASP.NET: Raise Event from User Control to Parent Page

"What is delegate?" we all have faced this question in one or more interview and the most common answer is "Function pointer". I have always been confused that what the hell are delegates? What to do with them in Web Development?? Now i came to realize the importance of delegates.

Here I am showing a simple example of delegate.
I have one user control and one aspx page. The user control contains one button. When user click on this button, I will call a method on main page using delegate.

Here is my user control:


C#
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs"
    Inherits="Delegate_WebUserControl" %>
<asp:Button ID="btnTest" runat="server" Text="I am Inside User Control" OnClick="btnTest_Click" />



VB.NET

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="Delegate_WebUserControl" %>
<asp:Button ID="btnTest" runat="server" Text="I am Inside User Control" OnClick="btnTest_Click" />


On WebUserControl.ascx.cs I have written simple delegate and event handler as shown below:

Code Behind

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Delegate_WebUserControl : System.Web.UI.UserControl
{
    // Delegate declaration 
    public delegate void OnButtonClick(string strValue);
    // Event declaration 
    public event OnButtonClick btnHandler;
    // Page load 
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
        // Check if event is null 
        if (btnHandler != null)
            btnHandler(string.Empty);
        // Write some text to output 
        Response.Write("User Control’s Button Click <BR/>");
    }
}


VB.NET
Partial Public Class Delegate_WebUserControl
    Inherits System.Web.UI.UserControl
    ' Delegate declaration 
    Public Delegate Sub OnButtonClick(strValue As String)
    ' Event declaration 
    Public Event btnHandler As OnButtonClick
    ' Page load 
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    End Sub
    Protected Sub btnTest_Click(sender As Object, e As EventArgs)
        ' Check if event is null 
        RaiseEvent btnHandler(String.Empty)
        ' Write some text to output 
        Response.Write("User Control’s Button Click <BR/>")
    End Sub
End Class


Above code first check whether btnHandler is not null and than raise the event by passing argument. You can pass any number of argument in event. You need to change
public delegate void OnButtonClick(string strValue) and btnHandler(string.Empty)
lines for changing number of arguments. Now take a look at aspx page:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Delegate_Default" %>

<%@ Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblText" Text="I am On Main Page : " runat="server"></asp:Label>
        <asp:DropDownList ID="ddlTemp" runat="server">
            <asp:ListItem>Talha</asp:ListItem>
            <asp:ListItem>Haris</asp:ListItem>
            <asp:ListItem>Ammar</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
    </div>
    </form>
</body>
</html>


Default.aspx has one drop down list and a user control as shown in above code. Lets look at Code Behind:

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Delegate_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Declare and Define Event of User Control. When User Clicks on button 
        //(which is inside UserControl) 
        // below event is raised as I have called raised that event on Button Click 
        WebUserControl1.btnHandler += new
        Delegate_WebUserControl.OnButtonClick(WebUserControl1_btnHandler);
    }
    void WebUserControl1_btnHandler(string strValue)
    {
        Response.Write("Main Page Event<BR/>Selected Value: " +
                    ddlTemp.SelectedItem.Text + "<BR/>");
    }
}

VB.NET


Partial Public Class Delegate_Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        ' Declare and Define Event of User Control. When User Clicks on button 
        '(which is inside UserControl) 
        ' below event is raised as I have called raised that event on Button Click 
        WebUserControl1.btnHandler += New Delegate_WebUserControl.OnButtonClick(WebUserControl1_btnHandler)
    End Sub
    Private Sub WebUserControl1_btnHandler(strValue As String)
        Response.Write("Main Page Event<BR/>Selected Value: " + ddlTemp.SelectedItem.Text + "<BR/>")
    End Sub
End Class


Now when you run the application and clicks on button you can see that when user click on button the user control raise the click event and calls the WebUserControl1_btnHandler(string strValue) method on main page.

Popular Posts