Control.RaiseBubbleEvent(Object, EventArgs) 方法

定義

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

protected:
 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)

參數

source
Object

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

args
EventArgs

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

範例

下列程式碼範例示範如何建立自訂類別 ChildControl,覆寫 Button.OnClick 方法,以呼叫將 Button.Click 事件傳送至其父 ASP.NET 伺服器控制項的 RaiseBubbleEvent 方法。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. 當使用者在包含 ChildControl實例的 ASP.NET 網頁中按一下按鈕時,它會在父控制項上引發 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) 
   {
      base.OnClick(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)
      MyBase.OnClick(e)
      Context.Response.Write("<br><br>ChildControl's OnClick called.")
      ' Bubble this event to parent.
      RaiseBubbleEvent(Me, e)
   End Sub
End Class

備註

RepeaterDataListGridView Web 控制項之類的 ASP.NET 伺服器控制項,都可以包含引發事件的子控制項。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.

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

適用於

另請參閱