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则不会引发该事件。

适用于

另请参阅