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:

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
}
	   

ssTk.co.uk
Last updated: 06 April 2012