c2c网站的功能,湖北专业的网瘾戒除学校地址,wordpress设置登录页面模板,wordpress设置页面加载js.NET Framework 开发员指南 事件冒泡ASP.NET 页框架提供一种称为“事件冒泡”的技术#xff0c;允许子控件将事件沿其包容层次结构向上传播。事件冒泡允许在控件层次结构中更方便的位置引发事件#xff0c;并且允许将事件处理程序附加到原始控件以及公开冒泡的事件的控件上。… .NET Framework 开发员指南 事件冒泡 ASP.NET 页框架提供一种称为“事件冒泡”的技术允许子控件将事件沿其包容层次结构向上传播。事件冒泡允许在控件层次结构中更方便的位置引发事件并且允许将事件处理程序附加到原始控件以及公开冒泡的事件的控件上。 数据绑定控件Repeater、DataList 和 DataGrid使用事件冒泡将子控件在项目模板内引发的命令事件公开为顶级事件。虽然 .NET Framework 中的 ASP.NET 服务器控件将事件冒泡用于命令事件事件数据类是从 CommandEventArgs 派生的事件但是服务器控件上定义的任何事件都可以冒泡。 控件可以通过从基类 System.Web.UI.Control 继承的两个方法参与事件冒泡。这两个方法是OnBubbleEvent 和 RaiseBubbleEvent。以下代码片段显示了这些方法的签名。[C#]
protected virtual bool OnBubbleEvent(object source,EventArgs args
);
protected void RaiseBubbleEvent(object source,EventArgs args
);
[Visual Basic]
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。以下代码片段摘自模板化数据绑定控件示例在检查事件参数的类型后使事件冒泡。 [C#]
protected override bool OnBubbleEvent(object source, EventArgs e) {if (e is CommandEventArgs) {// Adds information about an Item to the // CommandEvent.TemplatedListCommandEventArgs args new TemplatedListCommandEventArgs(this, source, (CommandEventArgs)e);RaiseBubbleEvent(this, args);return true;}return false;}
[Visual Basic]
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As BooleanIf 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 TrueEnd IfReturn False
End Function 控件停止事件冒泡并引发和/或处理该事件。引发事件需要调用将事件调度给侦听器的方法。若要引发冒泡的事件控件必须重写 OnBubbleEvent 以调用引发此冒泡的事件的 OnEventName 方法。引发冒泡的事件的控件通常将冒泡的事件公开为顶级事件。以下代码片段摘自模板化数据绑定控件示例引发一个冒泡的事件。 [C#]
protected override bool OnBubbleEvent(object source, EventArgs e) {bool handled false;if (e is TemplatedListCommandEventArgs) {TemplatedListCommandEventArgs ce (TemplatedListCommandEventArgs)e;OnItemCommand(ce);handled true;}return handled;
}
[Visual Basic]
Protected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As BooleanDim handled As Boolean FalseIf TypeOf e Is TemplatedListCommandEventArgs ThenDim ce As TemplatedListCommandEventArgs CType(e, TemplatedListCommandEventArgs)OnItemCommand(ce)handled TrueEnd IfReturn handled
End Function有关说明事件冒泡的示例请参见事件冒泡控件示例和模板化数据绑定控件示例。 注意 虽然启用事件冒泡的方法 OnBubbleEvent 符合用于引发事件的方法的标准 .NET Framework 命名模式但是没有名为 BubbleEvent 的事件。在停止事件冒泡的控件中将冒泡事件公开为顶级事件。例如DataList 控件将其模板中控件的 Command 事件公开为 ItemCommand 事件。另请注意在 .NET Framework 中 OnEventName 方法的标准签名有一个参数 (protected void OnEventName (EventArgs e))。但是OnBubbleEvent 有两个参数这是因为该事件起源于控件之外第二个参数提供源。 到目前为止本讨论说明了控件如何响应冒泡的事件。下面一节显示如何创作一个定义冒泡的事件的控件。 定义冒泡的事件 如果希望控件为它所定义的事件启用事件冒泡则控件必须从引发该事件的 OnEventName 方法调用 RaiseBubbleEvent。不需要在该控件中做额外的工作。以下代码片段显示了一个控件该控件定义了一个启用冒泡的 Command 事件。[C#]
protected virtual void OnCommand(CommandEventArgs e) {CommandEventHandler handler (CommandEventHandler)Events[EventCommand];if (handler ! null)handler(this,e);// The Command event is bubbled up the control hierarchy.RaiseBubbleEvent(this, e);}
[Visual Basic]
Protected Overridable Sub OnCommand(e As CommandEventArgs)Dim handler As CommandEventHandler CType(Events(EventCommand), CommandEventHandler)If Not (handler Is Nothing) Thenhandler(Me, e)End If The Command event is bubbled up the control hierarchy.RaiseBubbleEvent(Me, e)
End Sub 注意 事件冒泡并不限于命令事件。可以使用此处描述的机制使任何事件冒泡。 请参见 事件冒泡控件示例 | 模板化数据绑定控件示例 事件冒泡控件示例 下面的自定义控件 EventBubbler 说明了一种简单的事件冒泡情况。EventBubbler 是一个包含文本框 (TextBox)、按钮 (Button) 和标签 (Label) 控件的复合控件。EventBubbler 将命令事件从按钮冒泡到父容器控件自身并将它们公开为顶级事件。若要生成该示例请参见服务器控件示例中的说明。 有关更切合实际的示例请参见模板化数据绑定控件示例。[C#]
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace CustomControls
{public class EventBubbler : Control, INamingContainer {private int number 100;private Label label;private TextBox box1;private TextBox box2;public event EventHandler Click;public event EventHandler Reset;public event EventHandler Submit;public string Label{get{EnsureChildControls();return label.Text;}set{EnsureChildControls();label.Text value;}}public int Number{get{return number;}set{number value;}}public string Text1{get{EnsureChildControls();return box1.Text;}set{EnsureChildControls();box1.Text value;}}public string Text2{get{EnsureChildControls();return box2.Text;}set{EnsureChildControls();box2.Text value;}}protected override void CreateChildControls() {Controls.Add(new LiteralControl(h3Enter a number : ));box1 new TextBox();box1.Text 0;Controls.Add(box1);Controls.Add(new LiteralControl(/h3));Controls.Add(new LiteralControl(h3Enter another number : ));box2 new TextBox();box2.Text 0;Controls.Add(box2);Controls.Add(new LiteralControl(/h3));Button button1 new Button();button1.Text Click;button1.CommandName Click;Controls.Add(button1);Button button2 new Button();button2.Text Reset;button2.CommandName Reset;Controls.Add(button2);Button button3 new Button();button3.Text Submit;button3.CommandName Submit;Controls.Add(button3);Controls.Add(new LiteralControl(brbr));label new Label();label.Height 50;label.Width 500;label.Text Click a button.;Controls.Add(label);}protected override bool OnBubbleEvent(object source, EventArgs e) { bool handled false;if (e is CommandEventArgs){CommandEventArgs ce (CommandEventArgs)e;if (ce.CommandName Click){OnClick(ce);handled true; } else if (ce.CommandName Reset){OnReset(ce);handled true; }else if (ce.CommandName Submit){OnSubmit(ce);handled true; }}return handled; }protected virtual void OnClick (EventArgs e){if (Click ! null){Click(this,e);}}protected virtual void OnReset (EventArgs e){if (Reset ! null){Reset(this,e);}}protected virtual void OnSubmit (EventArgs e){if (Submit ! null){Submit(this,e);}} }
}
[Visual Basic]
Option Explicit
Option StrictImports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControlsNamespace CustomControlsPublic Class EventBubblerInherits ControlImplements INamingContainerPrivate _number As Integer 100Private _label As LabelPrivate _box1 As TextBoxPrivate _box2 As TextBoxPublic Event Click As EventHandlerPublic Event Reset As EventHandlerPublic Event Submit As EventHandlerPublic Property Label() As StringGetEnsureChildControls()Return _label.TextEnd GetSetEnsureChildControls()_label.Text valueEnd SetEnd PropertyPublic Property Number() As IntegerGetReturn _numberEnd GetSet_number valueEnd SetEnd PropertyPublic Property Text1() As StringGetEnsureChildControls()Return _box1.TextEnd GetSetEnsureChildControls()_box1.Text valueEnd SetEnd PropertyPublic Property Text2() As StringGetEnsureChildControls()Return _box2.TextEnd GetSetEnsureChildControls()_box2.Text valueEnd SetEnd PropertyProtected Overrides Sub CreateChildControls()Controls.Add(New LiteralControl(h3Enter a number : ))_box1 New TextBox()_box1.Text 0Controls.Add(_box1)Controls.Add(New LiteralControl(/h3))Controls.Add(New LiteralControl(h3Enter another number : ))_box2 New TextBox()_box2.Text 0Controls.Add(_box2)Controls.Add(New LiteralControl(/h3))Dim button1 As New Button()button1.Text Clickbutton1.CommandName ClickControls.Add(button1)Dim button2 As New Button()button2.Text Resetbutton2.CommandName ResetControls.Add(button2)Dim button3 As New Button()button3.Text Submitbutton3.CommandName SubmitControls.Add(button3)Controls.Add(New LiteralControl(brbr))_label New Label()_label.Height Unit.Pixel(50)_label.Width Unit.Pixel(500)_label.Text Click a button.Controls.Add(_label)End SubProtected Overrides Function OnBubbleEvent(source As Object, e As EventArgs) As BooleanDim handled As Boolean FalseIf TypeOf e Is CommandEventArgs ThenDim ce As CommandEventArgs CType(e, CommandEventArgs)If ce.CommandName Click ThenOnClick(ce)handled TrueElseIf ce.CommandName Reset ThenOnReset(ce)handled TrueElseIf ce.CommandName Submit ThenOnSubmit(ce)handled TrueEnd IfEnd If End IfEnd IfReturn handledEnd FunctionProtected Overridable Sub OnClick(e As EventArgs)RaiseEvent Click(Me, e)End SubProtected Overridable Sub OnReset(e As EventArgs)RaiseEvent Reset(Me, e)End SubProtected Overridable Sub OnSubmit(e As EventArgs)RaiseEvent Submit(Me, e)End SubEnd Class
End Namespace 在页上使用事件冒泡控件 下面的 ASP.NET 页使用自定义事件冒泡控件 EventBubbler并将事件处理程序附加到其顶级事件。[C#]
% Register TagPrefixCustom NamespaceCustomControls Assembly CustomControls %
html
script languageC# runatserverprivate void ClickHandler(Object sender,EventArgs e){MyControl.Label You clicked the b Click /b button;}private void ResetHandler(Object sender,EventArgs e){MyControl.Text1 0;MyControl.Text2 0; } private void SubmitHandler(Object sender,EventArgs e){if ( Int32.Parse(MyControl.Text1) Int32.Parse(MyControl.Text2) MyControl.Number)MyControl.Label h2 You won a million dollars!!!! /h2;else MyControl.Label Sorry, try again. The numbers you entered dont add up to the hidden number.;} /scriptbodyh1 The Mystery Sum Game /h1brform runatserver
Custom:EventBubbler id MyControl OnClick ClickHandler
OnReset ResetHandler OnSubmit SubmitHandler Number 10 runat server/
/form
/body
/html
[Visual Basic]
% Register TagPrefixCustom NamespaceCustomControls Assembly CustomControls %
html
script languageVB runatserverPrivate Sub ClickHandler(sender As Object, e As EventArgs)MyControl.Label You clicked the b Click /b buttonEnd SubPrivate Sub ResetHandler(sender As Object, e As EventArgs)MyControl.Text1 0MyControl.Text2 0End SubPrivate Sub SubmitHandler(sender As Object, e As EventArgs)If Int32.Parse(MyControl.Text1) Int32.Parse(MyControl.Text2) MyControl.Number ThenMyControl.Label h2 You won a million dollars!!!! /h2ElseMyControl.Label Sorry, try again. The numbers you entered dont add up to the hidden number.End IfEnd Sub /scriptbodyh1 The Mystery Sum Game /h1brform runatserver
Custom:EventBubbler id MyControl OnClick ClickHandler
OnReset ResetHandler OnSubmit SubmitHandler Number 10 runat server/
/form
/body
/html
转载于:https://www.cnblogs.com/laiwen/archive/2005/08/05/207921.html