Events in Visual Studio .NET

The Visual Studio .NET IDE can automatically handle much of the work required to implement events in ASP.NET. For example, it offers a list of all the possible events for each control and if you choose to implement an event, you can type in a name for the event handler. The IDE will create the boilerplate code necessary and will wire up the associated delegate. You have already seen some of these capabilities in this chapter, as well as the “manual” way of implementing events. In this section, you will see even easier ways to create event handlers in Visual Studio .NET.

Although events are handled in essentially the same way in C# and VB.NET under the hood, the syntax used by the two languages is very different. You can see this in the boilerplate created by Visual Studio .NET.

When a new web application is created in Visual Studio .NET, it automatically includes a code skeleton for the Page_Load event handler. A completed event handler was shown in Example 3-4 using VB .NET and in Example 3-5 using C#.

The event handler declaration in VB .NET has the form:

Private Sub Page_Load(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) _
                      Handles MyBase.Load

The Handles keyword indicates that this event handler method will handle the Load event of the base class.

In C#, the event handler declaration has the following syntax:

private void Page_Load(Object sender, System.EventArgs e)
{

Notice that there is no indication as to which event this method will ...

Get Programming ASP.NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.