Creating Event Handlers for an Object s Events


Creating Event Handlers for an Object's Events

In addition to methods and properties, objects may also have events. Events typically represent a state change or indicate that some action has transpired. For example, the ASP.NET Button Web control has a Click event which indicates that the user has performed some action, namely she's clicked the button in her browser, resulting in a postback. A good example of an event representing a state change is the TextBox Web control's TextChanged event. This event fires on postback if the TextBox's text content has been changed.

Often we'll want to have some code we've written run in response to a particular event firing. To accomplish this, we must create an event handler. An event handler is simply a subroutine with a particular set of input parameters that is wired to a particular event. This wiring process, which we'll examine shortly, causes the event handler to be executed whenever the event is raised.

All event handlers in a .NET program must be created as a subroutine and must accept precisely two input parameters: the first one must be of type Object, while the second one must be of a type derived from EventArgs. For example, the event handler for a Button Web control's Click event (which Visual Web Developer can create for us automatically) has the following signature:

[View full width]

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ... End Sub


This statement defines a subroutine named Button1_Click that serves as an event handler for Button1's Click event.

As you can see, the first parameter passed into the event handler is an Object. This Object parameter is the object that raised the event being handled. The second parameter is of type EventArgs. This second parameter can contain additional event-related information. In this event handler, as well as the page's Load event handler, no additional information is passed in. However, in future hours we will see examples of event handlers that are sent additional information through this second input parameter.

In addition to the input parameters, the event handler's definition also includes the Handles keyword. This is responsible for wiring the event handler to a particular event. In the case of the Button1_Click event handler, the subroutine is wired up to Button1's Click event. Button1 is an object representing the Button Web control defined in the page's HTML portion with ID Button1.

By the Way

You can create an event handler in Visual Basic by typing in the appropriate syntax by hand. However, Visual Web Developer removes this need because it can autogenerate event handler syntax. As we saw in previous hours, double-clicking a Web control in the Design view will create an event handler for the Web control's default event. Alternatively, you can always go to the source code portion and select the appropriate object and event from the drop-down lists at the top.





Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

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