BaseDataBoundControl.OnDataBound(EventArgs) 方法

定義

引發 DataBound 事件。Raises the DataBound event.

protected:
 virtual void OnDataBound(EventArgs ^ e);
protected virtual void OnDataBound (EventArgs e);
abstract member OnDataBound : EventArgs -> unit
override this.OnDataBound : EventArgs -> unit
Protected Overridable Sub OnDataBound (e As EventArgs)

參數

e
EventArgs

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

範例

下列程式碼範例將示範如何 OnDataBound 使用方法,透過衍生的 DataBound 資料繫結控制項類別來引發事件。The following code example demonstrates how the OnDataBound method is used to raise the DataBound event by a derived data-bound control class. 使用 GetDataPerformDataBinding 方法將資料系結至控制項之後,資料繫結控制項 DataBound 就會引發事件,以通知資料系結已完成。After using the GetData and PerformDataBinding methods to bind data to the control, the data-bound control raises the DataBound event to signal that data binding is complete. 這個程式碼範例是針對類別提供之較大範例的一部分 DataBoundControlThis code example is part of a larger example provided for the DataBoundControl class.

protected override void PerformSelect() {            

   // Call OnDataBinding here if bound to a data source using the
   // DataSource property (instead of a DataSourceID), because the
   // databinding statement is evaluated before the call to GetData.       
    if (! IsBoundUsingDataSourceID) {
        OnDataBinding(EventArgs.Empty);
    }            
    
    // The GetData method retrieves the DataSourceView object from  
    // the IDataSource associated with the data-bound control.            
    GetData().Select(CreateDataSourceSelectArguments(), 
        OnDataSourceViewSelectCallback);
    
    // The PerformDataBinding method has completed.
    RequiresDataBinding = false;
    MarkAsDataBound();
    
    // Raise the DataBound event.
    OnDataBound(EventArgs.Empty);
}
Protected Overrides Sub PerformSelect()

    ' Call OnDataBinding here if bound to a data source using the 
    ' DataSource property (instead of a DataSourceID) because the 
    ' data-binding statement is evaluated before the call to GetData.
    If Not IsBoundUsingDataSourceID Then
        OnDataBinding(EventArgs.Empty)
    End If

    ' The GetData method retrieves the DataSourceView object from the 
    ' IDataSource associated with the data-bound control.            
    GetData().Select(CreateDataSourceSelectArguments(), _
        AddressOf OnDataSourceViewSelectCallback)

    ' The PerformDataBinding method has completed.
    RequiresDataBinding = False
    MarkAsDataBound()

    ' Raise the DataBound event.
        OnDataBound(EventArgs.Empty)

End Sub

備註

這個方法會通知伺服器控制項,與控制項相關聯的任何資料系結邏輯都已完成。This method notifies a server control that any data binding logic associated with the control has completed.

重要

DataBind方法會在衍生自的所有控制項上密封 BaseDataBoundControlThe DataBind method is sealed on all controls derived from BaseDataBoundControl. 資料繫結控制項應覆寫, PerformDataBinding 而不是 DataBind 方法來系結資料。Data-bound controls should override PerformDataBinding instead of the DataBind method to bind data. 如果覆 DataBind 寫,則 OnDataBindingOnDataBound 事件會依順序引發。If DataBind is overridden, the OnDataBinding and OnDataBound events are raised out of order.

引發事件會透過委派叫用此事件處理常式。Raising an event invokes the event handler through a delegate. 如需詳細資訊,請參閱 處理和引發事件For more information, see Handling and Raising Events.

OnDataBound 方法也允許衍生類別處理事件,而不用附加委派。The OnDataBound method also allows derived classes to handle the event without attaching a delegate. 這是在衍生類別中處理事件的慣用技巧。This is the preferred technique for handling the event in a derived class.

給繼承者的注意事項

OnDataBound(EventArgs)在衍生類別中覆寫時,請務必呼叫基類的方法, OnDataBound(EventArgs) 以便註冊的委派會接收事件。When overriding OnDataBound(EventArgs) in a derived class, be sure to call the base class' OnDataBound(EventArgs) method so that registered delegates receive the event.

適用於

另請參閱