asp.net.ph

Skip Navigation LinksHome > ASP.NET Web Forms > Programming Web Forms > Web Forms Events and Handlers

Web Forms Events and Handlers

ASP.NET Web Forms   Programming Web Forms


Defining Web Forms Event-Handling Methods

When defining methods to handle events, there are basically two things you must know:

  • the name or names of the events supported by the control, and
  • the appropriate event-handling signature for the event you need to handle.

For example, the method to handle a Button control's Click event must have a signature similar to the following.

protected void methodName ( object src, EventArgs e ) 
  C# VB

IMPORTANT: Event-handling methods can have any name, but must have a signature that corresponds to the appropriate event-handler delegate signature. Otherwise, a compile-time error is thrown.

The following examples demonstrate how you can specify and code a handler for the Click event of a Button control.

Handling Control Action Events
Run Sample | View Source
Handling Multiple Control Action Events
Run Sample | View Source
Custom Paging Controls
Run Sample | View Source

The method signature depends on the type of the event object passed to the handler, which in turn depends on the control for which you are creating the event handler.

For example, the method to handle a Button control's Command event must have a signature similar to the following.

protected void methodName ( object src, CommandEventArgs e ) 
  C# VB

The following example demonstrates how you can specify and code a handler for the Command event of a Button control.

Custom Paging Controls
Run Sample | View Source

For information on specific event arguments, see the reference documentation for the control event you are working with.

When you already have an event handler with the appropriate signature, you can then bind the control event to it. There are basically two ways to attach handlers to events, declaratively at design time or programmatically at run time.

To attach event-handling methods at design time

  • Within the opening tag of the server control, assign the name of the method that will receive control when the event occurs. The general format is onEvent=method. For example, to attach a handler for a button control's click event:
    <asp:Button id="myButton" runat="server" onClick="methodName" />

To create an event handler at run-time using Visual Basic

  • Include an AddHandler statement, passing it the event to bind and the address of the method to call. Be sure that the statement is executed before the event can be raised — typically, you add handlers in the page initialization stage.
    AddHandler controlID.Click, AddressOf eventHandlerName

To create an event handler at run-time using C#

  • Create an instance of the EventHandler delegate, passing it the address of the method to bind to. Then add the delegate object to the list of methods called when the event is raised.
    this.controlID.Click += new System.EventHandler ( this.eventHandlerName );

The following example demonstrates how you can programmatically specify a common handler for the Command events of a Button control set.

// set common button properties

foreach ( Control c in header [ 1 ].Controls ) {
   ( ( Button ) c ) .Font.Bold = true;
   ( ( Button ) c ) .BackColor = System.Drawing.Color.DarkRed;
   ( ( Button ) c ) .ForeColor = System.Drawing.Color.Khaki;
   ( ( Button ) c ) .Command += new CommandEventHandler ( setPageCustom );
}
  C# VB
Custom Paging Controls
Run Sample | View Source
More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note