ASP.NET Application-Page Lifecycle Redux

By Peter Bromberg

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.

Popularity  (636 Views)
Picture
Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Follow Microsoft MVP
Create New Account
Article Discussion: ASP.NET Application-Page Lifecycle Redux
Peter Bromberg posted at Wednesday, February 18, 2009 3:10 PM
reply
Page model less important in Ajax apps
Mats Weiland replied to Peter Bromberg at Wednesday, October 27, 2010 3:07 AM
Is it just me or is the ASP.NET page model becoming more and more obsolete with the use of heavy client side apps that just load an empty DOM and do everything by Javascript? Any thoughts on that matter? I haven't used the page model for anything the last 1-2 years, I'm only using AJAX + .NET webservices...
reply
No its not just you.
Peter Bromberg replied to Mats Weiland at Wednesday, October 27, 2010 3:07 AM
JQuery has become very popular (it's even included in ASP.NET MVC) AJAX (Remote Scripting, really) is popular. However the ASP.NET Application / Page lifecycle is still there, and it runs every time a page is requested.
reply