服务器控件的学习-冒泡
asp.net页框架提供了"事件冒泡"技术.
允许子控件将事件沿其包容层次结构向上传播。
事件冒泡允许在控件层次结构中更方便的位置引发事件,
并且允许将事件处理程序附加到原始控件以及公开冒泡的事件的控件上。
数据绑定控件(Repeater、DataList 和 DataGrid)
使用事件冒泡将子控件(在项目模板内)引发的命令事件公开为顶级事件。
虽然 .NET Framework 中的 ASP.NET 服务器控件将事件冒泡用于命令事件(事件数据类是从CommandEventArgs 派生的事件)
但服务器控件上定义的任何事件都可以冒泡。
控件可以通过从基类 System.Web.UI.Control 继承的两个方法参与事件冒泡。
这两个方法是:OnBubbleEvent 和 RaiseBubbleEvent Overridable Protected Function OnBubbleEvent(ByVal source As
Object,ByVal args As EventArgs) As Boolean
Protected Sub RaiseBubbleEvent( ByVal source As Object, ByVal args As EventArgs)
RaiseBubbleEvent 的实现是由 Control 提供的,并且不能被重写。
RaiseBubbleEvent 沿层次结构向上将事件数据发送到控件的父级。
若要处理或引发冒泡的事件,控件必须重写 OnBubbleEvent 方法。
控件进行一些处理并继续使事件冒泡。
若要实现这一点,控件必须重写 OnBubbleEvent,并从 OnBubbleEvent 调用RaiseBubbleEvent。
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
If TypeOf e Is CommandEventArgs Then
' Adds information about an Item to the
' CommandEvent.
Dim args As New TemplatedListCommandEventArgs(Me, source, CType(e, CommandEventArgs))
RaiseBubbleEvent(Me, args)
Return True
End If
Return False
End Function
控件停止事件冒泡并引发和/或处理该事件。
引发事件需要调用将事件调度给侦听器的方法。
若要引发冒泡的事件,控件必须重写OnBubbleEvent 以调用引发此冒泡的事件的 OnEventName 方法。
引发冒泡的事件的控件通常将冒泡的事件公开为顶级事件。
以下代码片段引发一个冒泡的事件。
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
Dim handled As Boolean = False
If TypeOf e Is TemplatedListCommandEventArgs Then
Dim ce As TemplatedListCommandEventArgs = CType(e, TemplatedListCommandEventArgs)
OnItemCommand(ce)
handled = True
End If
Return handled
End Function
注意
虽然启用事件冒泡的方法 OnBubbleEvent 符合用于引发事件的方法的标准 .NET Framework 命名模式,
但是没有名为 BubbleEvent 的事件。
在停止事件冒泡的控件中,将冒泡事件公开为顶级事件。
例如,DataList 控件将其模板中控件的 Command 事件公开为 ItemCommand 事件。
另请注意,在 .NET Framework 中 OnEventName 方法的标准签名有一个参数 (protected void OnEventName (EventArgs e))。
但是,OnBubbleEvent 有两个参数,这是因为该事件起源于控件之外;第二个参数提供源。
定义冒泡的事件
Protected Overridable Sub OnCommand(e As CommandEventArgs)
Dim handler As CommandEventHandler = CType(Events(EventCommand), CommandEventHandler)
'这条语句看不懂
'假设commandeventhandler是一个委托
'<Serializable>Public Delegate Sub CommandEventHandler( ByVal sender As Object, ByVal e As CommandEventArgs )
'那么可以用dim将一个变量实例成一个委托的吗?
'通常不是只能用EVENT来声明的吗 public event Command as CommandEventHandler
'另外Events的用法,EventCommand是什么,都找不到答案
'还有通过ctype(events(eventcommand),commandeventhandler)可以换成一个委托的吗?
If Not (handler Is Nothing) Then
handler(Me, e)
End If
' The Command event is bubbled up the control hierarchy.
RaiseBubbleEvent(Me, e)
End Sub
完全代码:
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace CustomControls
Public Class EventBubbler
Inherits Control
Implements INamingContainer
Private _number As Integer = 100
Private _label As Label
Private _box1 As TextBox
Private _box2 As TextBox
Public Event Click As EventHandler
Public Event Reset As EventHandler
Public Event Submit As EventHandler
Public Property Label() As String
Get
EnsureChildControls()
Return _label.Text
End Get
Set
EnsureChildControls()
_label.Text = value
End Set
End Property
Public Property Number() As Integer
Get
Return _number
End Get
Set
_number = value
End Set
End Property
Public Property Text1() As String
Get
EnsureChildControls()
Return _box1.Text
End Get
Set
EnsureChildControls()
_box1.Text = value
End Set
End Property
Public Property Text2() As String
Get
EnsureChildControls()
Return _box2.Text
End Get
Set
EnsureChildControls()
_box2.Text = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Controls.Add(New LiteralControl("<h3>Enter a number : "))
_box1 = New TextBox()
_box1.Text = "0"
Controls.Add(_box1)
Controls.Add(New LiteralControl("</h3>"))
Controls.Add(New LiteralControl("<h3>Enter another number : "))
_box2 = New TextBox()
_box2.Text = "0"
Controls.Add(_box2)
Controls.Add(New LiteralControl("</h3>"))
Dim button1 As New Button()
button1.Text = "Click"
button1.CommandName = "Click"
Controls.Add(button1)
Dim button2 As New Button()
button2.Text = "Reset"
button2.CommandName = "Reset"
Controls.Add(button2)
Dim button3 As New Button()
button3.Text = "Submit"
button3.CommandName = "Submit"
Controls.Add(button3)
Controls.Add(New LiteralControl("<br><br>"))
_label = New Label()
_label.Height = Unit.Pixel(50)
_label.Width = Unit.Pixel(500)
_label.Text = "Click a button."
Controls.Add(_label)
End Sub
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As Boolean
Dim handled As Boolean = False
If TypeOf e Is CommandEventArgs Then
Dim ce As CommandEventArgs = CType(e, CommandEventArgs)
If ce.CommandName = "Click" Then
OnClick(ce)
handled = True
Else
If ce.CommandName = "Reset" Then
OnReset(ce)
handled = True
Else
If ce.CommandName = "Submit" Then
OnSubmit(ce)
handled = True
End If
End If
End If
End If
Return handled
End Function
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
Protected Overridable Sub OnReset(e As EventArgs)
RaiseEvent Reset(Me, e)
End Sub
Protected Overridable Sub OnSubmit(e As EventArgs)
RaiseEvent Submit(Me, e)
End Sub
End Class
End Namespace
经过重复的学习,还是有几个问题
只有先放一放,以后再寻求答案了