語言

ControlAdapter.OnInit(EventArgs) 方法

定義

覆蓋 OnInit(EventArgs) 相關控制的方法。

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

參數

e
EventArgs

一個 EventArgs 包含事件資料的 。

範例

以下程式碼範例是從該 ControlAdapter 類別衍生出一個自訂的控制介面卡。 接著它會 OnInit 覆寫設定相關控制項屬性的方法,並呼叫基礎方法完成控制初始化。

#using <System.Web.dll>
#using <System.dll>

using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::Adapters;

public ref class CustomControlAdapter: public ControlAdapter
{
   // Override the ControlAdapter default OnInit implementation.
protected:
   virtual void OnInit( EventArgs^ e ) override
   {
      // Make the control invisible.
      Control->Visible = false;
      
      // Call the base method, which calls OnInit of the control,
      // which raises the control Init event.
      ControlAdapter::OnInit( e );
   }
};
using System;
using System.Web.UI;
using System.Web.UI.Adapters;

public class CustomControlAdapter : ControlAdapter
{
    // Override the ControlAdapter default OnInit implementation.
    protected override void OnInit (EventArgs e)
    {
        // Make the control invisible.
        Control.Visible = false;

        // Call the base method, which calls OnInit of the control,
        // which raises the control Init event.
        base.OnInit(e);
    }
}
Imports System.Web.UI
Imports System.Web.UI.Adapters

Public Class CustomControlAdapter
    Inherits ControlAdapter

    ' Override the ControlAdapter default OnInit implementation.
    Protected Overrides Sub OnInit(ByVal e As EventArgs)

        ' Make the control invisible.
        Control.Visible = False

        ' Call the base method, which calls OnInit of the control, 
        ' which raises the control Init event.
        MyBase.OnInit(e)

    End Sub
End Class

備註

如果有介面卡連接到物件 Control 且 OnInit 該方法被覆寫,則會呼叫覆寫方法而非方法本身 Control.OnInit 。

覆寫 OnInit 以執行控制生命週期階段 Initialize 的目標特定處理。 通常,這些功能是在建立控制項時執行的。

給繼承者的注意事項

當你從類別繼 ControlAdapter 承且介面卡覆寫該 OnInit(EventArgs) 方法時,介面卡必須呼叫對應的基底類別方法,而基底類別再呼叫該 OnInit(EventArgs) 方法。 若未呼叫該 OnInit(EventArgs) 方法, Init 事件將不會被觸發。

適用於

另請參閱