Alex Mueller on Software and Technology 
Tuesday, July 17, 2007

True or false: in the ASP.NET 2.0 page lifecycle, OnPreRender is always called? Now what if the user control's visibility is set to false?

Recently, a developer asked me, "isn't OnPreRender called each time in the page lifecycle?" My initial response was "yes," but as we discovered, "not always." OnPreRender and CreateChildControls are only called in a UserControl when the visibility of that control is set to true.

There are several sites on the web with information about the ASP.NET page lifecycle for 1.* and 2.0. The basic methods in 1.* usually make my list of interview questions, and occasionally, interviewees can provide enough information to reveal a intimate knowledge of these. Since there are so many sites on the web with their own flavor of the ASP.NET page lifecycle, I will not reproduce it here. However, I will provide two displays that show a scenario I ran to investigate this.

To test this, I created a simple web application with one WebForm1.aspx, and two user controls, WebUserControl1.ascx and WebUserControl2.ascx. I have overridden a number of methods, but not all, and I have added a Response.Write() in each, which looks something like the following. I have even kept both OnLoad() and Page_Load(), just to see the order in which these methods are called.

protected override void OnPreRender(EventArgs e)
{
Response.Write(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod() + "<br/>");
base.OnPreRender(e);
}

My results are the following for when both user controls have their visibility set to "true" in the WebForm1.aspx file.

WebForm1::Void OnPreInit(System.EventArgs)
WebUserControl1::Void OnInit(System.EventArgs)
WebUserControl2::Void OnInit(System.EventArgs)
WebForm1::Void OnInit(System.EventArgs)
WebForm1::Void OnInitComplete(System.EventArgs)
WebForm1::Void OnPreLoad(System.EventArgs)
WebForm1::Void OnLoad(System.EventArgs)
WebForm1::Void Page_Load(System.Object, System.EventArgs)
WebUserControl1::Void OnLoad(System.EventArgs)
WebUserControl1::Void Page_Load(System.Object, System.EventArgs)
WebUserControl2::Void OnLoad(System.EventArgs)
WebUserControl2::Void Page_Load(System.Object, System.EventArgs)
WebForm1::Void OnLoadComplete(System.EventArgs)
WebForm1::Void CreateChildControls()
WebForm1::Void OnPreRender(System.EventArgs)
WebUserControl1::Void CreateChildControls()
WebUserControl1::Void OnPreRender(System.EventArgs)
WebUserControl2::Void CreateChildControls()
WebUserControl2::Void OnPreRender(System.EventArgs)
WebForm1::Void OnPreRenderComplete(System.EventArgs)
WebForm1::System.Object SaveViewState()
WebUserControl1::System.Object SaveViewState()
WebUserControl2::System.Object SaveViewState()
WebForm1::Void OnSaveStateComplete(System.EventArgs)
WebForm1::Void RenderControl(System.Web.UI.HtmlTextWriter)
WebForm1::Void Render(System.Web.UI.HtmlTextWriter)
WebForm1::Void RenderChildren(System.Web.UI.HtmlTextWriter)

The results are below for when the first user control has its visibility set to "false." Notice that CreatChildControls() and OnPreRender() are not called since they have no child controls to create, nor HTML to render.

WebForm1::Void OnPreInit(System.EventArgs)
WebUserControl1::Void OnInit(System.EventArgs)
WebUserControl2::Void OnInit(System.EventArgs)
WebForm1::Void OnInit(System.EventArgs)
WebForm1::Void OnInitComplete(System.EventArgs)
WebForm1::Void OnPreLoad(System.EventArgs)
WebForm1::Void OnLoad(System.EventArgs)
WebForm1::Void Page_Load(System.Object, System.EventArgs)
WebUserControl1::Void OnLoad(System.EventArgs)
WebUserControl1::Void Page_Load(System.Object, System.EventArgs)
WebUserControl2::Void OnLoad(System.EventArgs)
WebUserControl2::Void Page_Load(System.Object, System.EventArgs)
WebForm1::Void OnLoadComplete(System.EventArgs)
WebForm1::Void CreateChildControls()
WebForm1::Void OnPreRender(System.EventArgs)
WebUserControl2::Void CreateChildControls()
WebUserControl2::Void OnPreRender(System.EventArgs)

WebForm1::Void OnPreRenderComplete(System.EventArgs)
WebForm1::System.Object SaveViewState()
WebUserControl1::System.Object SaveViewState()
WebUserControl2::System.Object SaveViewState()
WebForm1::Void OnSaveStateComplete(System.EventArgs)
WebForm1::Void RenderControl(System.Web.UI.HtmlTextWriter)
WebForm1::Void Render(System.Web.UI.HtmlTextWriter)
WebForm1::Void RenderChildren(System.Web.UI.HtmlTextWriter)

A few more observations include the following. The order in which the user controls are rendered in the HTTP lifecycle is related to the order in which they are included in the web form. In my scenarios above, WebUserControl1 occurs before WebUserControl2 in the WebForm1's HTML, and as a result, its events are fired before the second user control. It is unnecessary to include Page_Load() when OnLoad() is present, however, I kept it to see the order in which they are called, with OnLoad() occurring first.

This is one of those exercises that helps one understand the order of events in the page lifecycle. Working with ASP.NET for five years now, I feel pretty familiar with them, and with ASP.NET 2.0, there are several more. Reading about these events in documentation is one thing, but testing them out in a application reaffirms their relationship in the page lifecycle.

Tuesday, July 17, 2007 8:43:22 PM (Mountain Standard Time, UTC-07:00) | Comments [0] | Technology#
Comments are closed.
MuellerDesigns.net
Search
On This Page
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Caring for the Team
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska