Before I get into this topic, lets look at a few definitions
Event: An event in C# is an occurrence. For example if you click a button, that is a button click event.
Subscribing to an Event: Stating which method needs to be called when an event occurs
Event Handler: The method that will be executed when an event occurs (or raised).
Delegate: Delegate is a function pointer. Check this out for a discussion on delegates.
How to subscribe to an event
If you want to subscribe to the SelectedIndexChanged event of a dropdown (ddlDept) in the code behind, this is the syntax to do it
In the line of code above:
This is the general syntax for consuming any event
EventName+=new DelegateName(MethodName)
The publisher defines the delegate and the event. The subscriber, defines the event handler (a method).
How to create and raise an event and consume it somewhere else
Suppose you have a user control. In this user control you want to raise an event on a button click, which you want to handle in the page containing the user control. This is a way to do it.
This is your code in the usercontrol ascx (Publisher)
The aspx page that holds user control (Subscriber)
The code behind for the aspx
using System;
Event: An event in C# is an occurrence. For example if you click a button, that is a button click event.
Subscribing to an Event: Stating which method needs to be called when an event occurs
Event Handler: The method that will be executed when an event occurs (or raised).
Delegate: Delegate is a function pointer. Check this out for a discussion on delegates.
How to subscribe to an event
If you want to subscribe to the SelectedIndexChanged event of a dropdown (ddlDept) in the code behind, this is the syntax to do it
ddlDept.SelectedIndexChanged+=new EventHandler(ddlDept_SelectedIndexChanged);
In the line of code above:
Code
|
What it means
|
ddlDept.SelectedIndexChanged
|
The event that you are subscribing to
|
EventHandler
|
A delegate that is defined in the .net framework
|
ddlDept_SelectedIndexChange
|
The name of the method (or function) you want to call when the event occurs
|
EventName+=new DelegateName(MethodName)
The publisher defines the delegate and the event. The subscriber, defines the event handler (a method).
How to create and raise an event and consume it somewhere else
Suppose you have a user control. In this user control you want to raise an event on a button click, which you want to handle in the page containing the user control. This is a way to do it.
This is your code in the usercontrol ascx (Publisher)
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<asp:Button ID="Button1" runat="server" Text="ClickMe"
onclick="Button1_Click" />
The code behind for the user control
using System;
using System.Web;
public partial class MyUserControl : System.Web.UI.UserControl
{
//Define the delagate (The method handling this event would need to have the same signature)
public delegate void myDelegate(string s);
//Define the event that needs to be raised
public event myDelegate myEvent;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (myEvent!=null)
{
string s = "Hello from usercontrol!";
//The event myEvent is raised in the next line
myEvent.Invoke(s);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/MyUserControl.ascx" TagPrefix="UserControl" TagName="EventEg" %>
<!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>Event example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<UserControl:EventEg id="ucEventEg" runat="server" />
</div>
</form>
</body>
</html>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Subscribe to the user control's event named myEvent
ucEventEg.myEvent += new MyUserControl.myDelegate(UserCortolButtonClicked);
}
private void UserCortolButtonClicked(string s)
{
Response.Write(s);
}
}
When you run the web application, this is how the page would look like
When you run the web application, this is how the page would look like
Nice article !
ReplyDelete