Every control that you add to your Web Form is capable of raising events. In order to capture these events and do something based on them occurring, you need to create an Event Handler.

Let's start with a standard ASP.Net Button. The default event raised by this control is the click event. The easiest way to set up the event handler for this event is to double click the button within the Design View of Visual Studio:

Name:  Design View - Button.png
Views: 3275
Size:  2.0 KB

Once you do this, you will be taken to the Code Behind page for you application, and you will see the Click Event Handler already written out for you.

C#
Name:  C# Button Click Event.png
Views: 3255
Size:  2.8 KB

VB.Net
Name:  VB Button Click Event.png
Views: 3283
Size:  3.2 KB

Now that is the default Click Event now being handled, but how do you access the other Events that the Button can raise?

If you are using VB.Net, this can be done in the Code Behind View by using the two drop down lists at the top of the page. Select the control that you are interested in the first drop down list, and then the event in the second drop down list:

Name:  VB Code Behind Event Handlers.png
Views: 3411
Size:  19.0 KB

In C#, this functionality if not included, however, there is an alternative, which always work in VB.Net. Switch back to Design View, and select your Button. Then bring up the Properties Window. (If this isn't visible, either right click on the Button and select properties, or select View | Properties Window, from the main menu). Notice the yellow thunder bolt. Click on this, and you will see all of the Events that can be raised by the button.

Name:  C# VB Properties Window Events.png
Views: 3312
Size:  8.9 KB

Simply double click in the TextBox to the right of the Event that you want, and this will switch you to the Code Behind view what the event handler already stubbed out.

One important thing to note here, is that you also have the ability to select an event handler that has already been created. Let's say you have two buttons on your page, and you want them both to use the same event handler. For the first button, use the technique described above, however, for the second button, rather than double clicking in the Textbox for the event, only single click in it. This will then provide a drop down list of event handlers that already exist, and you can select one to use for this second button.

So far, we have been talking about controls which are placed on your page. However, the page itself actually also raises events. In VB.Net, it is simple again to hook up to these events, using the two drop down list in the Code Behind view. However, in C#, it is not immediately obvious how you can hook up these events. Here is how this can be achieved:

  • Bring your aspx page up in Visual Studio
  • Select View | Component Designer from the Main Menu
  • Go to your Properties Window and select the yellow thunder bolt


This will show you all the events that your page can raise, and from here you can use them as before for your controls events.