在一個單獨的頁面中,執行順序為



  1. PreInit 

  2. Init 

  3. InitComplete 

  4. PreLoad 

  5. Load 

  6. LoadComplete 

  7. PreRender 

  8. PreRenderComplete

在頁面有MasterPage的時候



  1. ContentPage.PreInit
  2. Master.Init
  3. ContentPage.Init
  4. ContentPage.InitComplete 
  5. ContentPage.PreLoad 
  6. ContentPage.Load 
  7. Master.Load 
  8. ContentPage.LoadComplete 
  9. ContentPage.PreRender 
  10. Master.PreRender

注意上面高亮的部分,除此之外其他的地方一般是先MasterPageContentPage,這裏恰恰相反


那麼控制項的載入和頁面的幾個事件的關係是什麼呢?


一般情況下,頁面中的控制項會先於OnInit執行,亦即



  1.  Master中控制項的Init 
  2. ContentPage中控制項的Init 
  3. Master.Init 
  4. Content.Init 
  5. Content.Load 
  6. Master.Load 
  7. Master.中用戶控制項load 
  8. ContentPage頁面中的用戶控制項的 page_load

可以看到控制項的InitPageInit前執行,所以在Init中我們就可以放心使用頁面中的控制項了,但是下面的情況不同


如果你的頁面中使用WebControl,或者從WebControl繼承的控制項,WebControl需要使用CreateChildControls()來載入子控制項,這個函數會在這個控制項的Init後被調用,也就是說在一個WebControlOnInit,是沒有辦法直接使用其中的控制項的,不過我們也可以自己用FindControl把控制項載入進來,WebControl的其他方法的載入順序見下表































Event



Description



Init



You can use the OnInit method to trap this event and initialize member variables and other values.



LoadViewState



You can trap this event to customize how the control retrieves information from the ASP.NET hidden ViewState field.



Load



This event is raised after you create and initialize the control. This is the best place to make the connection to the database or load document content. You can trap the Load event by adding the OnLoad method to the class.



PreRender



This event is raised before ASP.NET renders the control. Any changes to the control's state are saved into the hidden ViewState field.



SaveViewState



This event is raised before the control state is persisted into the hidden ViewState field. You can trap the event to customize how this information is stored.



Render



You can use the Render method to respond to this event and specify the HTML code that represents the component's content.



Dispose



This event is useful for cleanup operations. It's raised before the control is torn down and is the best place to free the resources created during the load phase.



Unload



This event is raised before the control is torn down. The official documentation says not to use this event to perform cleanup, and to rely on the Dispose event instead.



 

arrow
arrow
    全站熱搜

    nelman 發表在 痞客邦 留言(0) 人氣()