Introduction
The event handling-based model is used in ASP.NET Web forms. We have the ability to create any server control event. We can design an event handler to handle the click event in the instance of the button control, for example. Even ASPX pages have their own set of page life-cycle events. We all know that when a user requests an ASPX page, all of its controls are rendered as HTML controls.
In this blog, we'll learn an event handling mechanism at work.
Event Handling in ASP.NET
Events is the means by which a process communicates. For example, interrupts are caused by the system. The application should be able to respond to and manage events when they occur. Examples of events are a mouse click, keypress, changed text, button click, mouse movements, or any other system-generated notification.
In ASP.NET, client-side events are raised and handled by the server. For example, the user might click a browser button. Then a click event occurs. The browser handles this client-side event, which sends it to the server to be processed.
The event handler is a function on the server that describes what to execute when an event is raised. As a result, when the event message is sent to the server, it checks to see if the click event has a handler associated with it. The event handler is executed if it has.
WebForms is benefited from ASP.NET's feature of event handling. It enabled us to create an event-based model for our app. We can add a simple button to an ASP.NET Web Forms page and then construct an event handler for a button's click event as a simple example. ASP.NET WebForms support both client and server events.
On the other hand, events linked with server controls in ASP.NET Web Forms pages originate on the client but are handled by the ASP.NET on the Web server.
The event-handler methods in ASP.NET WebForms follow a standard.Net Framework approach. All events take two arguments: an object that represents the object that triggered the event and an event object that contains any event-specific data.
Arguments in Event Handling
The ASP.NET event handlers require two parameters and return void. The object that is raising the event is represented by the first parameter, and the event argument is represented by the second parameter.
An event's general syntax is as follows:
private void EventName(object sender, EventArgs e);
Application Events
Some of the application events are:
- Application_Init: When an application initializes for the first time, the Application Init event is triggered.
- Application_Start: When an application starts for the first time, the Application Start event is fired.
- Application_BeginRequest: Each time a new request arrives, the Application BeginRequest event is triggered.
- Application_EndRequest: When an application closes, the Application EndRequest event is triggered.
- Application_AuthenticateRequest: A request is ready to be authenticated when the Application AuthenticateRequest event occurs. This event can be used to check for the user's responsibilities and rights if you're using Forms Authentication.
- Application_Error: When an unhandled error occurs in the application, the Application Error event is triggered.
- Application_End: When the application closes or times out, the Application End event is the last of its kind. It usually provides cleanup logic for the program.
Session Events
Some of the session events are:
- Session_Start: When a user's session is started for the first time, the Session Start event is fired. This is usually where session startup logic code is kept.
- Session_End: When a single user Session expires or times out, the Session End Event is triggered.
Page and Control Events
Some of the common page and control events are:
- Disposed: When the page or control is released, it is raised.
- Data Binding: When a control binds to a data source, it raises the DataBinding event.
- Init: When the page or control is initialized, it raises the init event.
- Error: When an unhandled exception is thrown, an error is thrown as a page event.
- Load: When a page or control is loaded, this event is triggered.
- PreRender: When the page or control is about to be rendered, the PreRender event is triggered.
- Unload: When a page or control is unloaded from memory, this event is triggered.