3.9 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 handle. That connection is made in the block of code inside the region labeled Web Form Designer generated code. Expanding that region reveals a method called InitializeComponent, which is called from within the OnInit event handler. Inside the InitializeComponent method is the following line of code:

this.Load += new System.EventHandler(this.Page_Load);

This line adds the Page_Load method to the Load delegate, causing the method to be invoked every time that event is raised.

Earlier, you saw how an attribute for the OnItemDataBound event was declaratively added to the DataGrid shown in Figure 3-2.

OnItemDataBound="OnItemDataBoundEventHandler"

The OnItemDataBoundEventHandler method was implemented in Example 3-7 in VB .NET and in Example 3-8 in C#.

To have Visual Studio .NET do more of the work for you, do not declare the event attribute in the .aspx file, as was done previously. Instead, you'll let the designer help you implement the event. The easiest event to implement in Visual Studio.NET is the designated "default" event, designated by the designer of the control.

To create an event handler for the default event, simply double-click on the control from within the design window. The boilerplate code for that event handler will automatically be created in the code-behind page and the cursor will be placed inside the event handler method, ready for you to enter your implementing code. The default event for the web controls are listed in Table 3-4.

Table 3-4. Default events for ASP.NET Controls

Control

Default Event

AdRotator

AdCreated

Button

Click

Calendar

SelectionChanged

CheckBox

CheckedChanged

CheckBoxList

SelectedIndexChanged

DataGrid

SelectedIndexChanged

DataList

SelectedIndexChanged

DropDownList

SelectedIndexChanged

HyperLink

Click

ImageButton

Click

Label

none

LinkButton

Click

ListBox

SelectedIndexChanged

RadioButton

CheckedChanged

RadioButtonList

SelectedIndexChanged

Repeater

ItemCommand

Creating event handlers for non-default events is different in Visual Studio.NET for C# than it is for VB.NET.

In C#, select the control in Design view, then click on the yellow lighting bolt at the top of the Properties window. All the possible events for that control will be listed and the default event will be highlighted. There are then several ways to create an event handler:

  • Double-clicking on the cell next to any event name will automatically create an event handler skeleton for that event with a default name, add that event handler to the event delegate, and open the code window with the cursor in the code skeleton, ready to type.

  • Alternatively, you can enter any event handler name you choose in the cell next to the event, and a code skeleton with that name will be created and hooked up, just like for a default name.

  • Finally, you can click in the cell next to an event name. An arrow will appear, and when you click on the arrow a list of all existing event handler methods will appear, from which you can choose a handler for the event.

It is possible for a single event handler to handle events from several different controls. For example, you may have a generic button click event handler that handles all the buttons on your form. The button that raised the event can be determined by testing the value of the sender parameter. In the following code snippet, a button click event handler casts the sender object (that is, the control which raised the event) to a Button type, then assigns the ID property of that button to a string variable that you can use in an if or switch statement.

private void BtnClick(object sender, System.EventArgs e) {    Button b = (Button)sender;    String buttonID  = b.ID; }

In VB .NET, all the event handlers are listed using the two drop-down controls at the top of the code-behind page. The control whose event you wish to handle is selected from the left drop-down, then the desired event is selected from the right drop-down. Click on an event to create an event handler with a default name. To use a non-default name, simply change the name of the method after it is created for you.

Since VB .NET uses the Handles keyword to wire up the event handler with the control's event, you can create generic event handlers by adding additional events to the Handles keyword, comma-separated. For example, the following VB .NET code snippet handles the click event for three buttons, displaying the Text property of the clicked button in a label control.

Private Sub btn1_Click(ByVal sender As Object, _                        ByVal e As System.EventArgs) _                        Handles btn1.Click, btn2.Click, btn3.Click    dim btn as Button = CType(sender,Button)    lblMsg.Text = btn.Text End Sub

Again, as with the C#, you must cast the sender object to a Button type, in this language using the CType method.

It is also possible to wire up event handlers in VB .NET using the AddHandler method rather than the Handles keyword. However, since that technique is not used by Visual Studio .NET, it will not be discussed further.



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net