参考: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:Declare an event by using the Event statement.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.UINamespace myControlsPublic Class RandomEvent
Inherits ControlPublic 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 SubProtected Sub OnEvent2( e As EventArgs )
  RaiseEvent Event2( Me, EventArgs.Empty )
End SubProtected 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 SubEnd ClassEnd NamespaceThe 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 SubThis 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 SubAn instance of the Random class is used to randomly return either the number 0 or 1. If 0 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 SubSub 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.

解决方案 »

  1.   

    问题是我这个不是控件的事件啊,我是想增加页面的事件,没有任何事件数据。也就是page类的子类的事件而不是control 类的子类的事件。
      

  2.   

    //基类
    protected override OnInit(EventArgs e){
        OnShow(e);
        base.OnInit(e);
    }
    public virtual void OnShow(EventArgs e){
    if(Show!=null){
    Show(this,e);
    }
    }
    //在子类中重载 OnShow()事件,达到修改目的