Chapter 8A. The Page Life Cycle in Web Forms

Every time a browser requests a page from a web application using the ASP.NET Web Forms framework, the page must complete the full page life cycle. In this lesson I list all of the steps in the page life cycle, show you how to run a trace of your page to see what is happening during each stage of the page's life cycle, and show you how to determine if a page is a postback.

All of the lessons so far have only used the Page_Load() event to add code to the page. Many more events are available during the page's life cycle. This is the sequence of events, in the order that they are raised, whenever a page is requested:

  • PreInit

  • Init

  • InitComplete

  • PreLoad

  • Load

  • LoadComplete

  • PreRender

  • PreRenderComplete

  • SaveStateComplete

  • Render

  • Unload

    Note

    Render is included on the list of events even though it is not an event; it is a method. The Page object calls the Render method on each control to write out the control's markup to the page.

PAGE-LEVEL TRACING

A simple way to view the page life cycle for your page is to enable tracing. You enable page-level tracing by adding a trace="true" attribute to the @Page directive at the top of the page. This is an example of an @Page directive that has been modified to enable tracing:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
    AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="Lesson8a._Default" Trace="true" %>

Figure 8A-1 shows the Request Details and Trace Information that is added to the bottom ...

Get ASP.NET 4 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.