Asp.Net request - event sequence
Given the Global & Page objects detailed below, the event flow is as follows for a new page request.
==== Begin request for url: http://localhost/APage.aspx
<Authenticate request> { authenticated user: Zorac\simon }
<ResolveRequestCache>
APage::ctor()
<AcquireRequestState>
<PreRequestHandlerExecute>
APage::OnInit()
APage::Page_Init()
APage::Page_Load()
APage::Page_PreRender()
<PostRequestHandlerExecute>
<ReleaseRequestState>
<UpdateRequestCache>
==== End request.
And event flow for a post back.
==== Begin request for url: http://localhost/APage.aspx
<Authenticate request> { authenticated user: Zorac\simon }
<ResolveRequestCache>
APage::ctor()
<AcquireRequestState>
<PreRequestHandlerExecute>
ManageLicenses::OnInit()
ManageLicenses::Page_Init()
ManageLicenses::LoadPageStateFromPersistenceMedium()
ManageLicenses::LoadViewState()
ManageLicenses::Page_Load()
ManageLicenses::Page_PreRender()
<PostRequestHandlerExecute>
<ReleaseRequestState>
<UpdateRequestCache>
==== End request.
Dynamic Controls and View State - nicked from MSDN iirc
When a control is created dynamically at run time, some information about the control is stored in the view state that is rendered with the page. When the page is posted back to the server, however, non-dynamic controls (those defined on the page) are instantiated in the Page.Init event and view state information is loaded before the dynamic controls can be recreated (generally in the Page_Load handler). Effectively, before the dynamic controls are recreated, view state is temporarily out of sync with the page's controls. After the Page_Load event has run, but before control event handling methods are called, the remaining view state information is loaded into the dynamically created controls.
In most scenarios, this view state processing model works fine. Typically, you add dynamic controls to the end of a container's collection of controls. The view state information stored for the dynamic controls is therefore extra information at the end of the view state structure for the appropriate container, and the page can ignore it until after the controls are created.
However, view state information about dynamically created controls can be a problem in two scenarios:
- If you insert dynamic controls between existing controls.
- If you insert controls dynamically and then reinsert them during a round trip, but with different values.
- If you insert dynamic controls between existing controls, the dynamic control's view state information is inserted into the corresponding location of the view state structure. When the page is posted and the view state is loaded, the dynamic control does not yet exist; therefore, the extra information in view state does not correspond to the right control. The result is usually an error indicating an invalid cast.
- If you reinsert controls with each round trip, each generation of dynamically created controls will pick up property values from the view state of the preceding set of controls. In many cases, you can avoid this problem by setting the EnableViewState property of the container control to false. In that case, no information about the dynamic controls is saved, and there is no conflict with successive versions of the controls.
Therefore, to retrieve information for dynamically created controls, create them in the page load event and process them in a button click event, or if not using a button event, their data is available in the pre render event.
Handy templates for ASP - with debugging reporting
public class Global : System.Web.HttpApplication
{
private System.ComponentModel.IContainer components = null;
public Global()
{
InitializeComponent();
}
#region App start & end
protected void Application_Start(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("App start up");
}
protected void Application_End(Object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("App exit");
}
#endregion
#region Pre-Request events
protected void Application_BeginRequest(Object sender, EventArgs e)
{
System.Diagnostics.Debug.Write( "==== Begin request for url: " );
System.Diagnostics.Debug.WriteLine( Request.Url.ToString() );
System.Diagnostics.Trace.Indent();
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if( User == null || !User.Identity.IsAuthenticated || User.Identity.Name == "" )
return;
System.Diagnostics.Debug.WriteLine(
"<Authenticate request> { authenticated user: " + User.Identity.Name + " }" );
}
protected void Application_ResolveRequestCache(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine( "<ResolveRequestCache>" );
}
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine( "<AcquireRequestState>" );
}
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine( "<PreRequestHandlerExecute>" );
System.Diagnostics.Trace.Indent();
}
#endregion
#region Post page events
protected void Application_PostRequestHandlerExecute(Object sender, EventArgs e)
{
System.Diagnostics.Trace.Unindent();
System.Diagnostics.Debug.WriteLine( "<PostRequestHandlerExecute>" );
}
protected void Application_ReleaseRequestState(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine( "<ReleaseRequestState>" );
}
protected void Application_UpdateRequestCache(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine( "<UpdateRequestCache>" );
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
System.Diagnostics.Trace.IndentLevel = 0;
System.Diagnostics.Debug.WriteLine("==== End request.");
}
#endregion
#region Other application events
protected void Application_Error(Object sender, EventArgs e)
{
}
#endregion
#region Session events
protected void Session_Start(Object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("<Session start> ");
}
protected void Session_End(Object sender, EventArgs e)
{
}
#endregion
#region Web Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
public class APage : System.Web.UI.Page
{
protected override object LoadPageStateFromPersistenceMedium()
{
System.Diagnostics.Debug.WriteLine("APage::LoadPageStateFromPersistenceMedium()");
return base.LoadPageStateFromPersistenceMedium();
}
protected override void LoadViewState(object savedState)
{
System.Diagnostics.Debug.WriteLine("APage::LoadViewState()");
base.LoadViewState(savedState);
}
#region Event Handlers
protected void Page_Init(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("APage::Page_Init()");
}
protected override void Page_Load(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("APage::Page_Load()");
base.Page_Load(sender, e);
}
protected void Page_PreRender(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine("APage::Page_PreRender()");
}
#endregion
public APage()
{
System.Diagnostics.Debug.WriteLine("APage::ctor()");
this.Init += new System.EventHandler(this.Page_Init);
this.PreRender += new System.EventHandler(this.Page_PreRender);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
System.Diagnostics.Debug.WriteLine("APage::OnInit()");
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
