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 會覆寫方法,則會呼叫 override 方法,而不是 Control.OnInit 方法。

覆寫 OnInit 以在控制生命週期的階段中 Initialize 執行目標特定處理。 一般而言,這些是建立控制項時所執行的函式。

給繼承者的注意事項

當您繼承自 ControlAdapter 類別,而配接器會覆寫 OnInit(EventArgs) 方法時,配接器必須呼叫對應的基類方法,進而呼叫 OnInit(EventArgs) 方法。 OnInit(EventArgs)如果未呼叫 方法, Init 將不會引發 事件。

適用於

另請參閱