
110
|
Chapter 5: ASP.NET Server Controls
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
While you can name your event handling procedures whatever you like, it’s
common to use the
ObjectName_EventName convention, which makes it very easy to
immediately see which procedures are event handlers. All event handlers are
passed an Object argument that is a reference to the control from which the event
was fired. They are also passed an instance of the
EventArgs class or of a class that
derives from EventArgs. For example, the OnCommand event of a Button control
passes an argument of type CommandEventArgs, which contains information
about the command represented by the button:
Sub MyButton_Command(Sender As Object, E As CommandEventArgs)
Message.Text = "Command " & E.CommandName & _
"was sent."
End Sub
As with creating controls, two techniques are available for creating and wiring up
event handlers in ASP.NET: declarative and programmatic.
Wiring Up Events in Declarative Tags
The technique typically used to wire up events that are handled in a server-side
<script> block, which is probably the simplest way to wire an event handler, is to
add the appropriate attribute to the declarative tag used to create the control. The
following code snippet sets the OnClick event handler to SubmitBtn_Click:
<asp:button id="MyBtn" text="Submit" onclick="MyBtn_Click" ...