Examining Custom Controls and Events


Every control has the following six standard events:

  • DataBinding This event is typically raised when the DataBind method is called.

  • Disposed This event occurs when the control is released from memory. This is a good place to close expensive resources, such as database connections.

  • Init This event occurs when the control is first initialized .

  • Load This event occurs when the control is loaded into the Page object.

  • PreRender This event occurs right before the control's Render method is called.

  • Unload This event occurs when the control is unloaded from memory.

The control in Listing 28.17, for example, illustrates how the Init , Load , and PreRender events can be handled.

Listing 28.17 ControlEvents.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ControlEvents Inherits Control Protected Overrides Sub OnInit( e As EventArgs )   Context.Response.Write( "<li> OnInit Event!" ) End Sub Protected Overrides Sub OnLoad( e As EventArgs )   Context.Response.Write( "<li> OnLoad Event!" ) End Sub Protected Overrides Sub OnPreRender( e As EventArgs )   Context.Response.Write( "<li> OnPreRender Event!" ) End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

In Listing 28.17, the protected control methods OnInit , OnLoad , and OnPreRender are overridden to display a message when each event occurs.

Adding Custom Events to a Control

Several of the standard ASP.NET controls raise custom events. For example, the AdRotator control raises an AdCreated event right before it renders a banner advertisement. The Calendar control raises a DayRender event right before it renders each day on the calendar.

You can add events to your own controls by completing the following two steps:

  1. Declare an event by using the Event statement.

  2. Raise the event by creating a subroutine that contains the RaiseEvent statement.

The control in Listing 28.18, for example, randomly raises one of two events.

Listing 28.18 RandomEvent.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class RandomEvent Inherits Control Public Event Event1( s As Object, e As EventArgs ) Public Event Event2( s As Object, e As EventArgs ) Protected Sub OnEvent1( e As EventArgs )   RaiseEvent Event1( Me, EventArgs.Empty ) End Sub Protected Sub OnEvent2( e As EventArgs )   RaiseEvent Event2( Me, EventArgs.Empty ) End Sub Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )   Dim objRandom As New Random   Dim intRanSelect As Integer   intRanSelect = objRandom.Next( 2 )   If intRanSelect = 0 Then     OnEvent1( EventArgs.Empty )   Else     OnEvent2( EventArgs.Empty )   End If End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

In Listing 28.18, two events are declared with the Event statement like this:

 
 Public Event Event1( s As Object, e As EventArgs ) Public Event Event2( s As Object, e As EventArgs ) 

The control contains two subroutines for raising these events. The first of these subroutines looks like this:

 
 Protected Sub OnEvent1( e As EventArgs )   RaiseEvent Event1( Me, EventArgs.Empty ) End Sub 

This subroutine simply raises Event1 by using the RaiseEvent statement. Two parameters are passed to the event: a reference to the current control and an empty EventArgs argument.

One or the other of these two subroutines is called within the Render method, which looks like this:

 
 Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )   Dim objRandom As New Random   Dim intRanSelect As Integer   intRanSelect = objRandom.Next( 2 )   If intRanSelect = 0 Then     OnEvent1( EventArgs.Empty )   Else     OnEvent2( EventArgs.Empty )   End If End Sub 

An instance of the Random class is used to randomly return either the number or 1 . If is returned, the OnEvent1 subroutine is called; otherwise , the OnEvent2 subroutine is called.

After you compile and copy the RandomEvent control to the application's \bin directory, you can wire up subroutines to the two events. The ASP.NET page in Listing 28.19 demonstrates how you can create subroutines that handle the two events.

Listing 28.19 DisplayRandomEvent.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="RandomEvent"%> <Script Runat="Server"> Sub doSomething1( s As Object, E As EventArgs )   Response.Write( "Event1 Raised!" ) End Sub Sub doSomething2( s As Object, E As EventArgs )   Response.Write( "Event2 Raised!" ) End Sub </Script> <html> <head><title>DisplayRandomEvent.aspx</title></head> <body> <myControls:RandomEvent   OnEvent1="doSomething1"   OnEvent2="doSomething2"   Runat="Server"/> </body> </html> 

The C# version of this code can be found on the CD-ROM.

When the RandomEvent control is declared, the doSomething1 subroutine is associated with the control's Event1 event, and the doSomething2 subroutine is associated with the control's Event2 event. Whenever you request the page, one or the other of the two subroutines executes.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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