代码示例

using System;

namespace myProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public string PageSteps = string.Empty;

        //Raised after the start stage is complete and before the initialization stage begins.
        protected void Page_PreInit(object sender, EventArgs e)
        {
            PageSteps += "1 - Page_PreInit<br>";

            //Access to page Controls not available in this step
            //Label1.Text = "Step 1";
        }

        //Raised after all controls have been initialized and any skin settings have been applied.
        //The Init event of individual controls occurs before the Init event of the page.
        protected void Page_Init(object sender, EventArgs e)
        {
            PageSteps += "2 - Page_Init<br>";

            Label1.Text = "Step 2";
        }

        //Raised at the end of the page's initialization stage.
        //Only one operation takes place between the Init and InitComplete events: tracking of view state changes is turned on.
        //View state tracking enables controls to persist any values that are programmatically added to the ViewState collection.
        //Until view state tracking is turned on, any values added to view state are lost across postbacks.
        //Controls typically turn on view state tracking immediately after they raise their Init event.
        protected void Page_InitComplete(object sender, EventArgs e)
        {
            PageSteps += "3 - Page_InitComplete<br>";

            Label1.Text = "Step 3";
        }

        //Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
        protected override void OnPreLoad(EventArgs e)
        {
            PageSteps += "4 - OnPreLoad<br>";

            Label1.Text = "Step 4";
        }

        //The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded.
        //The Load event of individual controls occurs after the Load event of the page.
        protected void Page_Load(object sender, EventArgs e)
        {
            PageSteps += "5 - Page_Load<br>";

            Label1.Text = "Step 5";
        }

        //Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Step only visible on PostBack
            PageSteps += "6 - btnSubmit_Click<br>";

            Label1.Text = "Step 6";
        }

        //Raised at the end of the event-handling stage.
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            PageSteps += "7 - Page_LoadComplete<br>";

            Label1.Text = "Step 7";
        }

        //Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
        //(To do this, the Page object calls EnsureChildControls for each control and for the page.)
        protected override void OnPreRender(EventArgs e)
        {
            PageSteps += "8 - OnPreRender<br>";

            Label1.Text = "Step 8";
        }

        //Raised after each data bound control whose DataSourceID property is set calls its DataBind method.
        protected override void OnPreRenderComplete(EventArgs e)
        {
            PageSteps += "9 - OnPreRenderComplete<br>";

            Label1.Text = "Step 9";
        }

        //Raised after view state and control state have been saved for the page and for all controls.
        //Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
        protected override void OnSaveStateComplete(EventArgs e)
        {
            PageSteps += "10 - OnSaveStateComplete<br><hr><br>";

            Label1.Text = "Step 10";
        }

        // Render
        //This is not an event; instead, at this stage of processing, the Page object calls this method on each control.
        //All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.

        //Raised for each control and then for the page.
        //Controls use this event to do final cleanup for specific controls, such as closing control-specific database connections
        protected void Page_UnLoad(object sender, EventArgs e)
        {
            //This last PageSteps addition will not be visible on the page
            PageSteps += "11 - Page_UnLoad<br>";

            //Access to page Controls not available in this step
            //Label1.Text = "Step 11";      
        }
    }
}

将以下代码添加到 .aspx 页面以可视化生命周期中的步骤。

<b>Page Life Cycle Visualization:</b>
<br />
<%= PageSteps %>

https://i.stack.imgur.com/Qn2xF.gif

更多信息