HtmlForm.OnInit(EventArgs) 方法


引發 Init 控制項的 HtmlForm 事件。Raises the Init event for the HtmlForm control.

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



包含事件資料的 EventArgsA EventArgs that contains event data.


OnInit 方法收到通知時,Web 服務器控制項必須執行建立和設定實例所需的任何初始化步驟。When notified by the OnInit method, Web server controls must perform any initialization steps that are required to create and set up an instance. 在伺服器控制項生命週期的這個階段中,尚未填入控制項的檢視狀態。In this stage of the server control's life cycle, the control's view state has yet to be populated. 此外,當呼叫這個方法時,您無法存取其他伺服器控制項,不論它是否為這個控制項的子系或父系。Additionally, you cannot access other server controls when this method is called, regardless of whether it is a child or parent to this control. 不保證會建立其他伺服器控制項,並可供存取。Other server controls are not guaranteed to be created and ready for access.

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

OnInit 方法也允許衍生類別處理事件,而不用附加委派。The OnInit 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.

OnInit 方法主要由控制項開發人員擴充 HtmlForm 控制項的功能所使用。The OnInit method is used primarily by control developers extending the functionality of the HtmlForm control.


覆寫衍生類別中的 OnInit(EventArgs) 方法時,請務必呼叫基類的 OnInit(EventArgs) 方法,讓已註冊的委派能接收事件。When overriding the OnInit(EventArgs) method in a derived class, be sure to call the base class's OnInit(EventArgs) method so that registered delegates receive the event. 此外,呼叫基類的 OnInit(EventArgs) 方法,可確保 HtmlForm 控制項已正確設定為網頁表單,並保存頁面的檢視狀態。Additionally, calling the base class's OnInit(EventArgs) method ensures that the HtmlForm control is properly set as the Web page form and the page's view state is persisted.