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.
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