|
|
Delegates in C# : Part 2
|
|
Delegates in .NET are usually used to map the event handlers to the events of a control. For example
|
-
In ASP.NET we have a Button control which exposes a click event.
When the user clicks the button on the webform, the event handler assosciated with the button is automatically invoked. The event handler is another method in the code behind file.
- The Button Click Event Handler method is associated with the click
event of the button with the help of a delegate. In ASP.NET 1.1 the association is present in the webform designer generated code. In ASP.NET 2.0 the association is prsent in the hidden partial class that is automatically generated by .NET
- In Visual Studio 2005 drag and drop a button control onto a webform. Double click on the button on the webform which will generate an event handler automatically in the code behind file. The signature of the event handler is shown below.
protected
void Button1_Click(object
sender, EventArgs e)
{
Response.Write("Button
Clicked");
}
- The event handler method has 2 parameters. The sender is the object that raised the event and the EventArgs parameter contains the data associated with the event.
- If you run the project and click the button, "Button Clicked" message will be shown on the webform.
- Now flip to the source view of the webform in Visual Studio. In the HTML you will find the OnClick attribute of the button is tied to Button1_Click event handler as shown below.
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- Now remove OnClick="Button1_Click" from the HTML, save and run the form. Click on the button, and we will see nothing happening. This is bcos we removed the association between the button click event and its Event handler.
- Now let us assign the Event Handler method to the click event of the button with the help of a delegate in the code behind file as shown below. In Part 1 we have seen how to create an instance of a delegate with the new keyword and pass a method reference to it. In the below code snippet we create an instance of EventHandler delegate and pass the reference of Button1_Click method. The delegate instance is then associated with the Click event of Button1 control using += operator. Another important point to remember is that, this code should be in the Page_Init event of a webform. Page Initialization is the first event in the event life cycle of a webform where all the controls are created and initialized.
protected
void Page_Init(object
sender, EventArgs e)
{
this.Button1.Click
+=new EventHandler(Button1_Click);
}
- We can add multiple event handlers to the click event as shown below. When you click the Button1 now, both the methods will be called.
protected
void Page_Init(object
sender, EventArgs e)
{
this.Button1.Click
+=new EventHandler(Button1_Click);
this.Button1.Click
+= new EventHandler(Button2_Click);
}
- To remove an event handler we use -= operator as shown below.
protected
void Page_Init(object
sender, EventArgs e)
{
this.Button1.Click
-= new EventHandler(Button1_Click);
}
|
|
Further Reading
|
|
|
Njoy Programming
ByPrasad Cherukuri
|
|
|