頁面請求的次序
當使用者請求一個用 Master Page 構建的頁面時,各種事件發生的次序如下:
Master Page 子控制項初始化;
內容頁面子控制項初始化;
Master Page 初始化;
內容頁面初始化;
內容頁面 Page_Load;
Master Page 的 Page_Load;
Master Page 子控制項載入;
內容頁面子控制項載入;
注意點:
因為內容頁面的 Page_Load 先於 Master Page 的 Page_Load,所以,如果要訪問 Master Page 裡的伺服器控制項,則必須在內容頁面的 Page_LoadComplete 方法裡書寫代碼。
asp.net頁面事件載入順序
Page 執行中將按照如下順序啟用事件:
Page.PreInit
Page.Init
Page.InitComplite
Page.PreLoad
Page.Load
Page.LoadComplete
Page.PreRender
Page.PreRenderComplete
如果頁面從另一個頁面繼承,如BasePage:System.Web.UI.Page,在BasePage中做了一些擴充,如許可權檢查,而其他頁面從BasePage繼承,則BasePage和最終Page的事件啟用順序是:
UI.PreInit
Page.PreInit
UI.Init
Page.Init
UI.InitComplite
Page.InitComplite
UI.PreLoad
Page.PreLoad
UI.Load
Page.Load
UI.LoadComplete
Page.LoadComplete
UI.PreRender
Page.PreRender
UI.PreRenderComplete
Page.PreRenderComplete
如果使用了MasterPage,則MasterPage中的事件和ContentPage中的事件按照下面順序啟用:
ContentPage.PreInit
Master.Init
ContentPage.Init
ContentPage.InitComplite
ContentPage.PreLoad
ContentPage.Load
Master.Load
ContentPage.LoadComplete
ContentPage.PreRender
Master.PreRender
ContentPage.PreRenderComplete
更進一步,如果ContentPage繼承BasePage,那麼,各事件的執行順序將變成:
UI.PreInit
ContentPage.PreInit
Master.Init
UI.Init
ContentPage.Init
UI.InitComplite
ContentPage.InitComplite
UI.PreLoad
ContentPage.PreLoad
UI.Load
ContentPage.Load
Master.Load
UI.LoadComplete
ContentPage.LoadComplete
UI.PreRender
ContentPage.PreRender
Master.PreRender
UI.PreRenderComplete
ContentPage.PreRenderComplete
瀏覽下來發現並不是我現在所學的asp.net 1.1,估計應該是asp.net 2.0,
不過也沒有關係,這讓我知道了他們有繼承時載入的順序。
即:先載入繼承頁的,在載入自己的,如果繼承頁有繼承則先載入繼承頁的繼承。
其實是個很簡單的內容。順便寫下Page事件(不知道1.1是不是就這些)
事件處理器名稱 |
發生時間 |
Page_Init |
在Web表單的檢視狀態載入伺服器控制項並對其初始化。 這是web表單生命週期的第一步 |
| Page_Load |
在Page對象上傳入伺服器控制項。由於此時檢視狀態資訊是可以使用的, 因此載這裡可以用代碼來改變空間的設定或者載頁面上顯示文本。 |
| Page_PreRender |
應用程式將要呈現Page對象 |
| Page_Unload |
頁面從記憶體中卸載 |
| Page_Error |
發生未處理的異常 |
| Page_AbortTransaction |
交易處理被終止 |
| Page_CommitTransaction |
交易處理被接受 |
| Page_DataBinding |
把頁面上的伺服器空間和資料來源綁定載一起 |
| Page_Disposed |
Page對象從記憶體中釋放掉。這是Page對象生命週期中的最後一個事件 |