.NET頁面事件執行順序

摘自:http://www.cnblogs.com/kenkofox/archive/2011/03/18/1987998.htmlhttp://blog.csdn.net/yiruoyun/article/details/4280732html

 

客戶發出POST請求-〉建立Page派生類,調用構造函數-〉調用Page類的IHttpHandler.ProcessRequest方法-〉激活Page類的Init事件-〉調用Page類的CreateChildControls虛方法-〉從POST變量和VIEWSTATE中還原服務器端控件狀態-〉激活Page類的Load事件-〉激活服務器端控件事件-〉激活Page類的PreRender事件-〉調用Page類的Render虛方法-〉調用Page類的RenderChildren虛方法-〉發送HTTP響應給客戶端-〉激活Page類的Unload事件-〉丟棄Page派生類的實例。
Page 執行中將按照以下順序激活事件:
Page.PreInit數據庫

Page.Init瀏覽器

Page.InitComplite 服務器

Page.PreLoad ide

Page.Load函數

Page.LoadComplete post

Page.PreRender測試

Page.PreRenderComplete
若是頁面從令一個頁面繼承,如BasePage:System.Web.UI.Page,在BasePage中作了一些擴展,如權限檢查,而其餘頁面從BasePage繼承,則BasePage和最終Page的事件激活順序是:
UI.PreInitspa

Page.PreInit .net

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

 

注意啊!!!Button1_Click處理返回數據前還有其餘處理!!!例如onPreLoad
哭啊,我本身以前不知道就被一個onPreLoad弄得莫名其妙~~~居然Button_Click函數中發現拿到的數據跟前臺post的數據不同~~


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region OnPreInit 第一步
    protected override void OnPreInit(EventArgs e)
    {
        //檢查 IsPostBack 屬性來肯定是否是第一次處理該頁。

        //建立或從新建立動態控件。

        //動態設置主控頁。

        //動態設置 Theme 屬性。

        //讀取或設置配置文件屬性值。

        //注意 
        //若是請求是回發請求,則控件的值還沒有從視圖狀態還原。若是在此階段設置控件屬性,則其值可能會在下一事件中被重寫。
 

        base.OnPreInit(e);
    }
    #endregion

    #region OnInit 第二步
    protected override void OnInit(EventArgs e)
    {
        //在全部控件都已初始化且已應用全部外觀設置後引起。使用該事件來讀取或初始化控件屬性。
        base.OnInit(e);
    }
    #endregion

    #region OnInitComplete 第三步
    protected override void OnInitComplete(EventArgs e)
    {
        //由 Page 對象引起。使用該事件來處理要求先完成全部初始化工做的任務。

        base.OnInitComplete(e);
    }
    #endregion

    #region PreLoad 第四步
    protected override void OnPreLoad(EventArgs e)
    {
        //若是須要在 Load 事件以前對頁或控件執行處理,請使用該事件。

        //在 Page 引起該事件後,它會爲自身和全部控件加載視圖狀態,而後會處理 Request 實例包括的任何回發數據。

        base.OnPreLoad(e);
    }
    #endregion

    #region OnLoad 第五步
    protected override void OnLoad(EventArgs e)
    {
        //Page 在 Page 上調用 OnLoad 事件方法,而後以遞歸方式對每一個子控件執行相同操做,如此循環往復,直到加載完本頁和全部控件爲止。
        //使用 OnLoad 事件方法來設置控件中的屬性並創建數據庫鏈接。

        base.OnLoad(e);
    }
    #endregion

    #region 控件事件 第六步
    protected void Button1_Click(object sender, EventArgs e)
    {
        //用這些事件來處理特定控件事件,如 Button 控件的 Click 事件或 TextBox 控件的 TextChanged 事件。

        //注意 
        //在回發請求中,若是頁包含驗證程序控件,請在執行任何處理以前檢查 Page 和各個驗證控件的 IsValid 屬性。
 

    }
    #endregion

    #region OnLoadComplete 第七步
    protected override void OnLoadComplete(EventArgs e)
    {
        //對須要加載頁上的全部其餘控件的任務使用該事件。

        base.OnLoadComplete(e);
    }
    #endregion

    #region OnPreRender 第八步
    protected override void OnPreRender(EventArgs e)
    {
        //在該事件發生前:

        //Page 對象會針對每一個控件和頁調用 EnsureChildControls。

        //設置了 DataSourceID 屬性的每一個數據綁定控件會調用 DataBind 方法。有關更多信息,請參見下面的數據綁定控件的數據綁定事件。

        //頁上的每一個控件都會發生 PreRender 事件。使用該事件對頁或其控件的內容進行最後更改。

        base.OnPreRender(e);
    }
    #endregion

    #region SaveStateComplete 第九步
    protected override void OnSaveStateComplete(EventArgs e)
    {
        //在該事件發生前,已針對頁和全部控件保存了 ViewState。將忽略此時對頁或控件進行的任何更改。

        //使用該事件執行知足如下條件的任務:要求已經保存了視圖狀態,但未對控件進行任何更改。

        base.OnSaveStateComplete(e);
    }
    #endregion

    #region Render 第十步
    //Render
    //這不是事件;在處理的這個階段,Page 對象會在每一個控件上調用此方法。全部 ASP.NET Web 服務器控件都有一個用於寫出發送給瀏覽器的控件標記的 Render 方法。

    //若是建立自定義控件,一般要重寫此方法以輸出控件的標記。不過,若是自定義控件只合並標準的 ASP.NET Web 服務器控件,不合並自定義標記,則不須要重寫 Render 方法。有關更多信息,請參見開發自定義 ASP.NET 服務器控件。

    //用戶控件(.ascx 文件)自動合併呈現,所以不須要在代碼中顯式呈現該控件。

    #endregion

    #region OnUnload 第十一步      
    protected override void OnUnload(EventArgs e)
    {
        //該事件首先針對每一個控件發生,繼而針對該頁發生。在控件中,使用該事件對特定控件執行最後清理,如關閉控件特定數據庫鏈接。

        //對於頁自身,使用該事件來執行最後清理工做,如:關閉打開的文件和數據庫鏈接,或完成日誌記錄或其餘請求特定任務。

        //注意 
        //在卸載階段,頁及其控件已被呈現,所以沒法對響應流作進一步更改。若是嘗試調用方法(如 Response.Write 方法),則該頁將引起異常。
 

        base.OnUnload(e);
    }
    #endregion
}

 

當頁面進行回發時,如點擊按鈕,以上事件都會從新執行一次,這時的執行順序爲:

1. OnPreInit
2. OnInit
3. OnInitComplete
4. OnPreLoad
5. Page_Load
6. OnLoad

7. Button_Click
8. OnLoadComplete
9. OnPreRender

能夠看到,Button_Click事件位於OnLoad以後執行,能夠測試一下:

public partial class TestControls : System.Web.UI.Page
    {
        static int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(count+ "Page_Load <br />");
            count++;
        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            Response.Write(count + "OnPreInit <br />");
            count++;
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Response.Write(count + "OnInit <br />");
            count++;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Response.Write(count + "OnLoad <br />");
            count++;
        }
        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            Response.Write(count + "OnPreLoad <br />");
            count++;
        }
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            Response.Write(count + "OnLoadComplete <br />");
            count++;
        }
        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
            Response.Write(count + "OnInitComplete <br />");
            count++;
        }
        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            Response.Write(count + "OnDataBinding <br />");
            count++;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Response.Write(count + "OnPreRender <br />");
            count++;
        }

        protected void btnGraphics_Click(object sender, EventArgs e)
        {
            //Bitmap bmp = new Bitmap(10, 10);
            //Graphics g = Graphics.FromImage(bmp);
            Response.Write(count + "btnGraphics_Click <br />");
            count++;
        }
    }
相關文章
相關標籤/搜索