Page Life Cycle In ASP.NET

Modified: 2008/08/22 14:09 by admin - Uncategorized
Edit

The "two minute guide" to the Page life-cycle model in ASP.NET

ASP.NET has something which is known as the "Page Life Cycle" which is at the very core of the ASP.NET WebControl model. It is nothing more than a very specific order of sequence in which different things are happening in the Page object on your web server. And for most ASP.NET developers (and also Gaia Ajax Widgets developers) most of the Page Life Cycle is not really that interesting, but there is one very important fact in the above code which is that the Page_Load method will run every time you call into your Page object. This means that every time you create either a postback or an Ajax Callback the Page_Load method will run.

This has several implications for you, first of all if the Page_Load method you have in your solutions are VERY slow and does "everything". Then your entire application will feel sluggish and degrade in regards to performance. Second of all the Page_Load method is a nice place to do initialization of things you want to happen EVERY time you execute code on your server.

Very often the Page_Load method is being used for things like initialization of your database connection, logging and so on. It is a nice place to put "initialization" logic of your application within. Though take care since if this method is slow then your ENTIRE APPLICATION will feel sluggish.

if (IsPostBack) ASP.NET has a way to separate a Postback/Callback from the "first initial hit" on your page. Very often you need special logic to run the first time a user hits your webpage. This might include logging, setting up default content on the page and so on. You can easily check to see if the current request is the "first" request or not by checking for the "IsPostBack" boolean property from the Page class in ASP.NET. Gaia Ajax Requests will also return true for IsPostBack checks so this is a very nice way to check if this is a callback/postback or the initial hit at your page from the user.

To check if it is a Gaia Ajax Callback you can use Gaia.WebWidgets.Manager.IsAjaxCallback property.

ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.