Event Handling In ASP.NET

Introduction

In ASP.NET Web forms, we are using an event-based model. We can create every server control event. For example, in the case of the button control, we can create an event handler to handle the click event. Even the ASPX page has its page life-cycle events. We all know that when an ASPX page is requested by the user, all its controls render as HTML controls. In this session, we are going to learn that there is a mechanism at work for the event handling.

What does event mean?

Event is a way for a class to provide the notifications to the clients of the class when something interesting happens to an object. We are basically familiar with the use of events in a user interface (UI) such as button click, changed text, etc. An event is a useful way for the objects to signal the state changes that may be useful to the clients of the object.


In simple terms, it’s like giving the instruction for an action.

event means

Therefore, let’s see how our familiar action, such as button click,  works for us. Every event associated with the server controls originate on the client browser but are handled on the Web Server by an ASP.NET page. How does ASP.NET handle this?



Let’s discuss what type of events we come across in our coding:
  • Events for the Server Controls.
    1. Protected void ButtonID_Click(Object sender,EventArgs e)  
  • Events for the Page Life Cycle.
    1. Protected void Page_Load(Object sender,EventArgs e)  
  • Application/Session Life Cycle Events.
    1. Protected void Application_Start(object sender, EventArgs e)  

Each function has three things in common:

  1. Object Argument - It represents the object that raised the event.
  2. Event Args - Event object contains any event specific information.
  3. Name of Event - All these follow a naming convention.

Hence, start with the naming convention, it helps ASP.NET to handle the event.

There is a property on the page level directive called AutoEventWireUp, which controls the automatic binding of the page events, based on the method of naming convention.

Another thing that works for us is Postback; the process of submitting the ASP.NET page back to the server (simply a subsequent request to a page). It says to the server that the client is posting data back to you for the processing. Some controls have AutoPostBack properties, while some do not. In this case, we define AutoPostBack property externally as true.

Last but not least, responding to the event is done by the client side script. Every control in ASP.NET renders an element to the Browser and that raises the client side events to handle it.


Similar Articles