How to create Event Handlers in ASP.NET

ASP.NET provides an event based programming model that simplifies Web programming. Controls in ASP.NET have a default event you typically handle. All GUI applications are incomplete without enabling actions. Even though arranging components is a significant issue, applying actions is also equally important. These actions are instructs the program to act when something happens. This is called events in programming language.

Events can also be generated without user interactions. Event handlers are methods in an object that are executed in response to some events occurring in the application. To understand the event handling model of .Net framework, we need to understand the concept of delegate and event. See my article on delegate and events on in the website with the following link:

The Page object default event is Load and the Button object default event is Click event. In Visual Studio View designer mode you can write up an event handler for the default event of a control by simply double clicking the control in View design mode. 

For example if you double click a page your will be taken to the Page_Load event in the code behind file of the page. The Page_Load event handler is triggered every time the web page is requested and loaded by the server.

Similarly if you double click on a Button or TextBox to generate a default event handler in your code behind file. You can do the same for other controls you can bind several control events to it.

How to add an event handler to a button?

I will explain how to write a event handler for button control using C#.

Step:1: Create a web application as File->New ->Project :


Step 2: Go to the design view and add a button control as:


Step 3: Select the button and right click and click on properties. In the properties window click the yellow icon event. The window will change like the figure below:


You can double click the the button, it will take you to code behind file where you can write the code if the user click button what action has to be performed. See the figure below:


I hope that this article would have helped you in understanding creating Event Handlers in ASP.NET using C# language. Please share it if you know more about this attribute. Your feedback and constructive contributions are welcome.