Saturday, June 15, 2013

Complete Lifecycle of an ASP.Net page and controls

Complete Lifecycle of an ASP.Net page and controls


Many people have blogged about the lifecycle of an ASP.Net Page or Master Page but every time I need to find the complete lifecycle of a control it’s not so easy. Today I wrote a couple of test pages to generate a reference.
The pages are very simple, I just overrode every method I could find and wrote to the trace. For completeness when the method called its base I wrapped it with “Start MethodName” and “End MethodName” (if you look at the trace outputs below you will see why). I did this for the Master Page (.master), Page (.aspx), User Control (.ascx) and Web Control (.cs), the page structre was simple:
  • Master Page
    • Page
      • User Control
      • Web Control
For clarity I’ve split the produced trace into 5 individual reference sections:

ASP.Net 2.0 Master Page Life Cycle

  • Start Construct()
  • End Construct()
  • Start FrameworkInitialize()
  • End FrameworkInitialize()
  • Start AddParsedSubObject(object obj)
    • CreateControlCollection()
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End AddParsedSubObject(object obj)
  • Start AddParsedSubObject(object obj)
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End AddParsedSubObject(object obj)
  • Start AddParsedSubObject(object obj)
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End AddParsedSubObject(object obj)
  • Start AddParsedSubObject(object obj)
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End AddParsedSubObject(object obj)
  • Start AddParsedSubObject(object obj)
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End AddParsedSubObject(object obj)
  • ResolveAdapter()
  • Start OnInit(EventArgs e)
  • End OnInit(EventArgs e)
  • Start TrackViewState()
  • End TrackViewState()
  • Start OnLoad(EventArgs e)
    • Page_Load(object sender, EventArgs e)
  • End OnLoad(EventArgs e)
  • Start EnsureChildControls()
    • ResolveAdapter()
    • Start CreateChildControls()
    • End CreateChildControls()
  • End EnsureChildControls()
  • Start OnPreRender(EventArgs e)
  • End OnPreRender(EventArgs e)
  • SaveViewState()
  • SaveViewState()
  • HasControls()
  • Start RenderControl(HtmlTextWriter writer)
    • Start Render(HtmlTextWriter writer)
      • Start RenderChildren(HtmlTextWriter writer)
      • End RenderChildren(HtmlTextWriter writer)
    • End Render(HtmlTextWriter writer)
  • End RenderControl(HtmlTextWriter writer)

ASP.Net 2.0 Page Life Cycle

  • ResolveAdapter()
  • DeterminePostBackMode()
  • Begin PreInit
    • Start OnPreInit(EventArgs e)
    • End OnPreInit(EventArgs e)
    • HasControls()
    • Start RemovedControl(Control control)
    • End RemovedControl(Control control)
    • ResolveAdapter()
    • Start AddedControl(Control control, int index)
    • End AddedControl(Control control, int index)
  • End PreInit
  • Begin Init
    • ResolveAdapter()
    • Start OnInit(EventArgs e)
    • End OnInit(EventArgs e)
    • Start TrackViewState()
    • End TrackViewState()
  • End Init
  • Begin InitComplete
    • Start OnInitComplete(EventArgs e)
    • End OnInitComplete(EventArgs e)
  • End InitComplete
  • Begin PreLoad
    • Start OnPreLoad(EventArgs e)
    • End OnPreLoad(EventArgs e)
  • End PreLoad
  • Begin Load
    • Start OnLoad(EventArgs e)
      • Page_Load(object sender, EventArgs e)
    • End OnLoad(EventArgs e)
  • End Load
  • Begin LoadComplete
    • Start OnLoadComplete(EventArgs e)
    • End OnLoadComplete(EventArgs e)
  • End LoadComplete
  • Begin PreRender
    • Start EnsureChildControls()
      • ResolveAdapter()
      • Start CreateChildControls()
      • End CreateChildControls()
    • End EnsureChildControls()
    • Start OnPreRender(EventArgs e)
    • End OnPreRender(EventArgs e)
  • End PreRender
  • Begin PreRenderComplete
    • Start OnPreRenderComplete(EventArgs e)
    • End OnPreRenderComplete(EventArgs e)
  • End PreRenderComplete
  • SaveViewState()
  • Begin SaveState
    • SaveViewState()
    • HasControls()
    • Start SavePageStateToPersistenceMedium(object state)
      • ResolveAdapter()
    • End SavePageStateToPersistenceMedium(object state)
  • End SaveState
  • Begin SaveStateComplete
    • Start OnSaveStateComplete(EventArgs e)
    • End OnSaveStateComplete(EventArgs e)
  • End SaveStateComplete
  • Begin Render
    • CreateHtmlTextWriter(System.IO.TextWriter tw)
    • Start RenderControl(HtmlTextWriter writer)
      • Start Render(HtmlTextWriter writer)
        • Start RenderChildren(HtmlTextWriter writer)
        • End RenderChildren(HtmlTextWriter writer)
      • End Render(HtmlTextWriter writer)
    • End RenderControl(HtmlTextWriter writer)
  • End Render
  • CreateHtmlTextWriter(System.IO.TextWriter tw)

ASP.Net 2.0 User Control Life Cycle

  • Start Construct()
  • End Construct()
  • Start FrameworkInitialize()
  • End FrameworkInitialize()
  • ResolveAdapter()
  • Start OnInit(EventArgs e)
  • End OnInit(EventArgs e)
  • Start TrackViewState()
  • End TrackViewState()
  • Start OnLoad(EventArgs e)
    • Page_Load(object sender, EventArgs e)
  • End OnLoad(EventArgs e)
  • Start EnsureChildControls()
    • ResolveAdapter()
    • Start CreateChildControls()
    • End CreateChildControls()
  • End EnsureChildControls()
  • Start OnPreRender(EventArgs e)
  • End OnPreRender(EventArgs e)
  • SaveViewState()
  • SaveViewState()
  • HasControls()
  • Start RenderControl(HtmlTextWriter writer)
    • Start Render(HtmlTextWriter writer)
      • Start RenderChildren(HtmlTextWriter writer)
      • End RenderChildren(HtmlTextWriter writer)
    • End Render(HtmlTextWriter writer)
  • End RenderControl(HtmlTextWriter writer)

ASP.Net 2.0 Web Control Life Cycle

  • Constructor Test()
  • Start ApplyStyleSheetSkin(Page page)
  • End ApplyStyleSheetSkin(Page page)
  • ResolveAdapter()
  • Start OnInit(EventArgs e)
  • End OnInit(EventArgs e)
  • Start TrackViewState()
  • End TrackViewState()
  • Start OnLoad(EventArgs e)
  • End OnLoad(EventArgs e)
  • Start EnsureChildControls()
    • ResolveAdapter()
    • Start CreateChildControls()
    • End CreateChildControls()
  • End EnsureChildControls()
  • Start OnPreRender(EventArgs e)
  • End OnPreRender(EventArgs e)
  • SaveViewState()
  • SaveViewState()
  • HasControls()
  • Start RenderControl(HtmlTextWriter writer)
    • Start Render(HtmlTextWriter writer)
      • Start RenderBeginTag(HtmlTextWriter writer)
        • Start AddAttributesToRender(HtmlTextWriter writer)
        • End AddAttributesToRender(HtmlTextWriter writer)
      • End RenderBeginTag(HtmlTextWriter writer)
    • Start RenderContents(HtmlTextWriter writer)
      • Start RenderChildren(HtmlTextWriter writer)
      • End RenderChildren(HtmlTextWriter writer)
    • End RenderContents(HtmlTextWriter writer)
    • Start RenderEndTag(HtmlTextWriter writer)
    • End RenderEndTag(HtmlTextWriter writer)
    • End Render(HtmlTextWriter writer)
  • End RenderControl(HtmlTextWriter writer)

Complete Trace

Category Message
Page ResolveAdapter()
Page DeterminePostBackMode()
aspx.page Begin PreInit
Page Start OnPreInit(EventArgs e)
Page End OnPreInit(EventArgs e)
MasterPage Start Construct()
MasterPage End Construct()
Page HasControls()
Page Start RemovedControl(Control control)
Page End RemovedControl(Control control)
MasterPage Start FrameworkInitialize()
MasterPage End FrameworkInitialize()
MasterPage Start AddParsedSubObject(object obj)
MasterPage CreateControlCollection()
MasterPage Start AddedControl(Control control, int index)
MasterPage End AddedControl(Control control, int index)
MasterPage End AddParsedSubObject(object obj)
MasterPage Start AddParsedSubObject(object obj)
MasterPage Start AddedControl(Control control, int index)
Page ResolveAdapter()
MasterPage End AddedControl(Control control, int index)
MasterPage End AddParsedSubObject(object obj)
MasterPage Start AddParsedSubObject(object obj)
MasterPage Start AddedControl(Control control, int index)
MasterPage End AddedControl(Control control, int index)
MasterPage End AddParsedSubObject(object obj)
UserControl Start Construct()
UserControl End Construct()
UserControl Start FrameworkInitialize()
UserControl End FrameworkInitialize()
CustomWebControl Constructor Test()
CustomWebControl Start ApplyStyleSheetSkin(Page page)
CustomWebControl End ApplyStyleSheetSkin(Page page)
MasterPage Start AddParsedSubObject(object obj)
MasterPage Start AddedControl(Control control, int index)
MasterPage End AddedControl(Control control, int index)
MasterPage End AddParsedSubObject(object obj)
MasterPage Start AddParsedSubObject(object obj)
MasterPage Start AddedControl(Control control, int index)
MasterPage End AddedControl(Control control, int index)
MasterPage End AddParsedSubObject(object obj)
Page Start AddedControl(Control control, int index)
Page End AddedControl(Control control, int index)
aspx.page End PreInit
aspx.page Begin Init
Page ResolveAdapter()
MasterPage ResolveAdapter()
UserControl ResolveAdapter()
UserControl Start OnInit(EventArgs e)
UserControl End OnInit(EventArgs e)
UserControl Start TrackViewState()
UserControl End TrackViewState()
CustomWebControl ResolveAdapter()
CustomWebControl Start OnInit(EventArgs e)
CustomWebControl End OnInit(EventArgs e)
CustomWebControl Start TrackViewState()
CustomWebControl End TrackViewState()
MasterPage Start OnInit(EventArgs e)
MasterPage End OnInit(EventArgs e)
MasterPage Start TrackViewState()
MasterPage End TrackViewState()
Page Start OnInit(EventArgs e)
Page End OnInit(EventArgs e)
Page Start TrackViewState()
Page End TrackViewState()
aspx.page End Init
aspx.page Begin InitComplete
Page Start OnInitComplete(EventArgs e)
Page End OnInitComplete(EventArgs e)
aspx.page End InitComplete
aspx.page Begin PreLoad
Page Start OnPreLoad(EventArgs e)
Page End OnPreLoad(EventArgs e)
aspx.page End PreLoad
aspx.page Begin Load
Page Start OnLoad(EventArgs e)
Page Page_Load(object sender, EventArgs e)
Page End OnLoad(EventArgs e)
MasterPage Start OnLoad(EventArgs e)
MasterPage Page_Load(object sender, EventArgs e)
MasterPage End OnLoad(EventArgs e)
UserControl Start OnLoad(EventArgs e)
UserControl Page_Load(object sender, EventArgs e)
UserControl End OnLoad(EventArgs e)
CustomWebControl Start OnLoad(EventArgs e)
CustomWebControl End OnLoad(EventArgs e)
aspx.page End Load
aspx.page Begin LoadComplete
Page Start OnLoadComplete(EventArgs e)
Page End OnLoadComplete(EventArgs e)
aspx.page End LoadComplete
aspx.page Begin PreRender
Page Start EnsureChildControls()
Page ResolveAdapter()
Page Start CreateChildControls()
Page End CreateChildControls()
Page End EnsureChildControls()
Page Start OnPreRender(EventArgs e)
Page End OnPreRender(EventArgs e)
MasterPage Start EnsureChildControls()
MasterPage ResolveAdapter()
MasterPage Start CreateChildControls()
MasterPage End CreateChildControls()
MasterPage End EnsureChildControls()
MasterPage Start OnPreRender(EventArgs e)
MasterPage End OnPreRender(EventArgs e)
UserControl Start EnsureChildControls()
UserControl ResolveAdapter()
UserControl Start CreateChildControls()
UserControl End CreateChildControls()
UserControl End EnsureChildControls()
UserControl Start OnPreRender(EventArgs e)
UserControl End OnPreRender(EventArgs e)
CustomWebControl Start EnsureChildControls()
CustomWebControl ResolveAdapter()
CustomWebControl Start CreateChildControls()
CustomWebControl End CreateChildControls()
CustomWebControl End EnsureChildControls()
CustomWebControl Start OnPreRender(EventArgs e)
CustomWebControl End OnPreRender(EventArgs e)
aspx.page End PreRender
aspx.page Begin PreRenderComplete
Page Start OnPreRenderComplete(EventArgs e)
Page End OnPreRenderComplete(EventArgs e)
aspx.page End PreRenderComplete
Page SaveViewState()
MasterPage SaveViewState()
UserControl SaveViewState()
CustomWebControl SaveViewState()
aspx.page Begin SaveState
Page SaveViewState()
Page HasControls()
MasterPage SaveViewState()
MasterPage HasControls()
UserControl SaveViewState()
UserControl HasControls()
CustomWebControl SaveViewState()
CustomWebControl HasControls()
Page Start SavePageStateToPersistenceMedium(object state)
Page ResolveAdapter()
Page End SavePageStateToPersistenceMedium(object state)
aspx.page End SaveState
aspx.page Begin SaveStateComplete
Page Start OnSaveStateComplete(EventArgs e)
Page End OnSaveStateComplete(EventArgs e)
aspx.page End SaveStateComplete
aspx.page Begin Render
Page CreateHtmlTextWriter(System.IO.TextWriter tw)
Page Start RenderControl(HtmlTextWriter writer)
Page Start Render(HtmlTextWriter writer)
Page Start RenderChildren(HtmlTextWriter writer)
MasterPage Start RenderControl(HtmlTextWriter writer)
MasterPage Start Render(HtmlTextWriter writer)
MasterPage Start RenderChildren(HtmlTextWriter writer)
UserControl Start RenderControl(HtmlTextWriter writer)
UserControl Start Render(HtmlTextWriter writer)
UserControl Start RenderChildren(HtmlTextWriter writer)
UserControl End RenderChildren(HtmlTextWriter writer)
UserControl End Render(HtmlTextWriter writer)
UserControl End RenderControl(HtmlTextWriter writer)
CustomWebControl Start RenderControl(HtmlTextWriter writer)
CustomWebControl Start Render(HtmlTextWriter writer)
CustomWebControl Start RenderBeginTag(HtmlTextWriter writer)
CustomWebControl Start AddAttributesToRender(HtmlTextWriter writer)
CustomWebControl End AddAttributesToRender(HtmlTextWriter writer)
CustomWebControl End RenderBeginTag(HtmlTextWriter writer)
CustomWebControl Start RenderContents(HtmlTextWriter writer)
CustomWebControl Start RenderChildren(HtmlTextWriter writer)
CustomWebControl End RenderChildren(HtmlTextWriter writer)
CustomWebControl End RenderContents(HtmlTextWriter writer)
CustomWebControl Start RenderEndTag(HtmlTextWriter writer)
CustomWebControl End RenderEndTag(HtmlTextWriter writer)
CustomWebControl End Render(HtmlTextWriter writer)
CustomWebControl End RenderControl(HtmlTextWriter writer)
MasterPage End RenderChildren(HtmlTextWriter writer)
MasterPage End Render(HtmlTextWriter writer)
MasterPage End RenderControl(HtmlTextWriter writer)
Page End RenderChildren(HtmlTextWriter writer)
Page End Render(HtmlTextWriter writer)
Page End RenderControl(HtmlTextWriter writer)
aspx.page End Render
Page CreateHtmlTextWriter(System.IO.TextWriter tw) Ref. 

Monday, May 6, 2013

Add selected Rows into Datatable from DataSet

  DataSet ds = new DataSet();
  ds = (DataSet)Session["dsFocusArea"];
  DataTable dt = ds.Tables[1].Clone();
  foreach (DataRow row in ds.Tables[1].Select("unitid = " + TextBox1.Text + ""))
  {
    dt.Rows.Add(row.ItemArray);
  }