After the reference master page, event page master pages and content pages of the execution order asp.net

Page_PreInit step 01, the contents page of
the first 02 steps, Page_Init master page
step 03, Page_Init content page
step 04, Page_InitComplete content page
step 05, Page_PreLoad content page
step 06, the contents of the page Page_Load
first 07 step, Page_Load master page
step 08, the master page or click buttons etc. page postback event (Master or content of button event does not trigger the same time)
the Page_LoadComplete step 09, the contents page of
step 10, the content Page_PreRender page
step. 11, master pages Page_PreRender
step 12, Page_PreRenderComplete content page
Page_SaveStateComplete step 13, contents of the page
in step 14, the master page Page_Unload
step 15, the contents of the page Page_Unload

In addition, reproduced a: asp.net page event execution order ( http://www.cnblogs.com/oletan/archive/2008/11/04/1326587.html#1549248 )

Note: For ease of understanding, you may think so, OnPreInit method is equivalent to Page_PreInit method, OnLoad method is equivalent to the Page_Load method

In reality, it is rewriting OnPreInit method calls inside Page_PreInit the above method, and so on, OnLoad overridden method is called above Page_Load method.

If OnLoad overridden method, and did not write base.OnLoad (e) statements, then the Page_Load method does.

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;

the _Default partial class public: Page 
{
    #region OnPreInit Step
    protected the override void OnPreInit (EventArgs E)
    {
        // check IsPostBack property to determine if the first page is processed.

        // create or re-create dynamic controls.

        // set dynamic master page.

        // Set the Theme property dynamically.

        // Read or set profile property values.

        Note //  
        // If the request is a postback request, the value of the control has not yet been restored from view state. If the setting control properties at this stage, its value might be overwritten in the next event.
 

        base.OnPreInit(e);
    }
    #endregion

    Step #region OnInit
    protected void the override the OnInit (EventArgs E)
    {
        // all controls are initialized and have applied all of the appearance after setting initiator. Use this event to read or initialize control properties.
        base.OnInit (E);
    }
    #endregion

    Step #region OnInitComplete
    protected the override void OnInitComplete (EventArgs E)
    {
        // caused by the Page object. Use this event to handle all the tasks required to complete the initialization.

        base.OnInitComplete(e);
    }
    #endregion

    The fourth step #region PreLoad
    protected the override void OnPreLoad (EventArgs E)
    {
        // If you need to perform processing on a page or control before the Load event, please use this event.

        // After Page triggered the event, it loads view state for themselves and all controls, and then handles any postback data included in the Request instance.

        base.OnPreLoad(e);
    }
    #endregion

    The fifth step #region OnLoad
    protected the override void the OnLoad (EventArgs E)
    {
        // Page calls on Page OnLoad event method, then recursively do the same for each child control, and so on ad infinitum, until finished loading the page and all controls until.
        // Use the OnLoad event method to set properties in controls and establish database connections.

        base.OnLoad(e);
    }
    #endregion

    a sixth step #region control events
    protected void the Button1_Click (SENDER Object, EventArgs E)
    {
        // event to handle these specific control events, such as control or Button Click event TextChanged event TextBox control.

        // Note  
        // postback request, if the page contains validator controls, check Page IsValid property and individual validation controls before performing any processing.
 

    }
    #endregion

    #region OnLoadComplete Step Seven
    protected the override void onLoadComplete (EventArgs E)
    {
        // use this event to all other controls on the tasks required to load the page.

        base.OnLoadComplete(e);
    }
    #endregion

    #region OnPreRender eighth step
    protected the OnPreRender the override void (EventArgs E)
    {
        // before the event:

        // Page object calls EnsureChildControls page for each control.

        // set the DataSourceID property of each data-bound control calls the DataBind method. For more information, please see the following data-bound controls data-binding event.

        For each control on the page // PreRender event occurs. Use this event to page content or control the final changes.

        base.OnPreRender (s);
    }
    #endregion

    #region SaveStateComplete Step Nine
    protected the override void OnSaveStateComplete (EventArgs E)
    {
        // Before this event occurs, ViewState has been saved for the page and all controls. We will ignore any changes at this time to be a page or control.

        // Use this event to perform tasks meet the following criteria: requirements have been saved view state, but did not make any changes to the controls.

        base.OnSaveStateComplete(e);
    }
    #endregion

    #region Render Step Ten
    // Render
    // this is not an event; at this stage of the process, Page object calls this method on each control. All ASP.NET Web server controls have a Render method to write the control sends to the browser marker.

    // If you create a custom control, typically override this method to output tag control. However, the merger only if the custom control standard ASP.NET Web server controls, do not merge custom tag, you do not need to override the Render method. For more information, see Developing Custom ASP.NET server controls.

    // user control (.ascx file) automatically merge rendering, it is not necessary to explicitly render the control code.

    #endregion

    #region OnUnload Step Eleven       
    protected the override void the OnUnload (EventArgs E)
    {
        // This event occurs for each control first, then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.

        // For the page itself, use this event to do final cleanup work, such as: close open files and database connections, or the completion of logging or other request for a particular task.

        // Note  
        // unloading stage, page and its controls have been rendered, and therefore can not make further changes to the response stream. If you try to call a method (such as Response.Write method), the page throws an exception.
 

        base.OnUnload(e);
    }
    #endregion
}

Published 16 original articles · won praise 1 · views 30000 +

Guess you like

Origin blog.csdn.net/wvtjplh/article/details/104148570