Understanding Event Handling in .NET

In general terms, an event is the resultant outcome of an action. In the programming terms, when the user does some action such clicks a button then the user is directed to some other window or dialog box.

If clicking a button is an action then the outcome of that action is known as event. In .NET Framework an event enables objects in a class to notify other objects that an action has been performed and they should react to this. Events in .NET Framework are based on publisher-subscriber model. The class that issues or publishes an event is called the publisher of the event and the class that registers to the published event is called the subscriber of the event.

When talking about events you should be aware of three most important concepts of events. They are event handlers, event arguments, and delegates. An event handler is a process of what and how the outcome should be displayed in response to the event notification. The event handlers take parameters similarly to the values that an event generates. By default the event handlers take two parameters and return no data. The first parameter describes the object on which the event occurred and the second parameter contains the event arguments.

The event arguments describe the data associated with an event. This data is sufficient enough to handle the event. The event arguments are of types EventArgs. When an event is raised, means when some button is clicked, the event arguments are transferred to the event handlers. Delegates in .NET Framework are used to attach code to the events. A delegate is a class that allows you to create objects that store the reference to methods that does not return any value and that accepts two arguments. Delegates can be compared to function pointers in other languages.

In ASP.NET you can handle events through three ways. These are:
• By overriding the virtual method of the base class
• By creating a delegate to the event
• By AutoEventWireUp of the Page class


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.