Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Event Delegation in asp.net

Posted by Viral Sarvaiya on August 10, 2010

Hear i m demonstrate the Event delegation in asp.net.

I am developing a simple asp.net project that handle the event of the web user control to the aspx page.

in simple means click event of the web user control’s button is handle in aspx page.

Create new website. there are default.aspx page

Add new Web User Control named “EventControl.ascx”

in the EventControl.ascx page write the following code.

<table border="1">
 <tr>
<td>
This is the Web User Control
</td>
 </tr>
 <tr>
<td>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="btn1_Click"></asp:Button>
</td>
 </tr>
</table>

in the EventControl.ascx.cs page

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

public partial class EventControl : System.Web.UI.UserControl
{
public delegate void BtnSubmitClickEventHandler(object sender, EventArgs e);
public event BtnSubmitClickEventHandler BtnSubmitClickEvent;

protected void btn1_Click(object sender, EventArgs e)
{
BtnSubmitClickEvent(sender, e);
}
}

means we have created event delegate in the EventControl.ascx.cs page now we can code it to default.aspx page.

means in the click event of the EventControl.ascx is written in the default.aspx page.

Now In the Default.aspx file


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="EventControl.ascx" TagName="EventControl" 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>
 <table>
 <tr>
 <td>
 Example of the Event Deligation
 </td>
 </tr>
 <tr>
 <td>
 <uc1:EventControl ID="EventControl1" runat="server" />
 </td>
 </tr>
 <tr>
 <td>
 <h3>
 this is the page 1</h3>
 </td>
 </tr>
 <tr>
 <td>
 <asp:Label ID="lblPrint" runat="server"></asp:Label>
 </td>
 </tr>
 </table>
 </div>
 </form>
</body>
</html>

now in the default.aspx.cs file. we are creating the handler of the event

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

public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 EventControl1.BtnSubmitClickEvent += new EventControl.BtnSubmitClickEventHandler(EventControl1_BtnSubmitClickEvent);
 }

 void EventControl1_BtnSubmitClickEvent(object sender, EventArgs e)
 {
 lblPrint.Text = "This Function is call From Event Delegation.";
 }
}

For this you can create your own delegate.

Advertisements

3 Responses to “Event Delegation in asp.net”

  1. Good post.

  2. great::

    check this one too:
    http://www.codeshode.com/2010/07/event-delegation-aspnet-raise-event.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: