ASP.NET Application-Page Lifecycle Redux

The ASP.NET Application and Page lifecycles are rich with a large number of events that are fired under various circumstances. Understanding how and under what circumstances these events take place can help ASP.NET developers to gain a better understanding of how an ASP.NET application works, and will serve as an aide to custom development.

What I have done here is to create a single-page ASP.NET application in which every available event -- both in the HttpApplication class, and in the Page class, is wired up and numbered in the order that it would normally occur. When an event is fired, a numbered line is emitted and added to an Application variable. Each line describes the order of firing, the event name, and has a hyperlink pointing to the MSDN Library documentation help page for the event.

In addition, since some events are fired after the page is rendered, I've provided three buttons on the page. The "Clear-Postback" button clears out the Application variable contents, creates a postback, and then displays all the events that fired after clearing. The "Recycle App" button actually unloads the AppDomain and re-requests the page so you can see the changes, and the Reload Page button actually reloads the page but without any postback.

Here is the codebehind for the two server-side buttons:

       protected void Button1_Click(object sender, EventArgs e)
        {
            Application["stuff"] = "";
            this.stuff.InnerHtml = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            // Recycle the app, but redirect to make the browser request the page again.
            // You should see "redirected=true" on the querystring in the browser address bar.
            System.Web.HttpRuntime.UnloadAppDomain();
            Response.Redirect("~/Default.aspx?redirected=true");
        }


And the "Reload page with no postback" button:

     
    <input id="Button3" type="button" value="Reload Page" onclick ="javascript:window.location.href=window.location.href;" />


When you download and run the Solution, you'll be presented with an initial page view that looks like this:

 

There are other events to study, but they are generally much more specialized and you can find out about them by reading the lifecyle pages linked at the top of the page in the sample web application.

I hope this is useful and gives you some good ideas.  Download the Visual Studio 2008 Solution.

By Peter Bromberg   Popularity  (1935 Views)