Control.RaiseBubbleEvent(Object, EventArgs) 方法


指派事件的任何來源和它的資訊至控制項的父控制項。Assigns any sources of the event and its information to the control's parent.

 void RaiseBubbleEvent(System::Object ^ source, EventArgs ^ args);
protected void RaiseBubbleEvent (object source, EventArgs args);
member this.RaiseBubbleEvent : obj * EventArgs -> unit
Protected Sub RaiseBubbleEvent (source As Object, args As EventArgs)



事件的來源。The source of the event.


包含事件資料的 EventArgs 物件。An EventArgs object that contains the event data.


下列程式碼範例示範如何建立自訂類別, ChildControl 並覆寫 Button.OnClick 方法以呼叫將事件傳送 RaiseBubbleEvent Button.Click 至其父 ASP.NET 伺服器控制項的方法。The following code example demonstrate how to create a custom class, ChildControl, overriding the Button.OnClick method to call the RaiseBubbleEvent method that sends the Button.Click event to its parent ASP.NET server control. 當使用者在包含實例的 ASP.NET 網頁中按一下按鈕時 ChildControl ,它會 OnBubbleEvent 在包含實例的父控制項上引發方法,並將字串「 ChildControl ChildControl 類別 OnClick 方法被呼叫」寫入頁面。When the user clicks a button in an ASP.NET page that includes an instance of ChildControl, it raises the OnBubbleEvent method on the parent control that contains the instance of ChildControl and writes the string "The ChildControl class OnClick method is called" to the page.

public class ChildControl : Button
   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
   protected override void OnClick(EventArgs e) 
      Context.Response.Write("<br><br>ChildControl's OnClick called.");
      // Bubble this event to parent.
      RaiseBubbleEvent(this, e);
Public Class ChildControl
   Inherits Button
   <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
   Protected Overrides Sub OnClick(e As EventArgs)
      Context.Response.Write("<br><br>ChildControl's OnClick called.")
      ' Bubble this event to parent.
      RaiseBubbleEvent(Me, e)
   End Sub
End Class


ASP.NET 伺服器控制項(例如 RepeaterDataListGridView Web 控制項)可以包含引發事件的子控制項。ASP.NET server controls such as the Repeater, DataList and GridView Web controls can contain child controls that raise events. 例如,控制項中的每個資料列 GridView 都可以包含一個或多個由範本動態建立的按鈕。For example, each row in a GridView control can contain one or more buttons created dynamically by templates. 來自嵌套控制項的事件是「反升」,而不是個別引發事件的每個按鈕,也就是將它們傳送至控制項的父系。Rather than each button raising an event individually, events from the nested controls are "bubbled" - that is, they are sent to the control's parent. 父代會輪流引發名 RowCommand 為且具有參數值的泛型事件。The parent in turn raises a generic event called RowCommand with parameter values. 這些值可讓您判斷引發原始事件的個別控制項。These values allow you to determine which individual control that raised the original event. 藉由回應這個單一事件,您可以避免為子控制項撰寫個別事件處理方法。By responding to this single event, you can avoid having to write individual event-handling methods for child controls.

當您無法覆寫這個方法時,您的作者可以藉由覆寫方法來處理或引發反升事件 OnBubbleEventWhile you cannot override this method, controls you author can handle or raise bubbled events by overriding the OnBubbleEvent method.