Monday, May 9, 2016

Loading Sequence of Master Page, Contents Page and User Control in ASP.NET

In this article I will show the loading process sequence of a Master Page that contains a Contents Page and that Contents Page has a User Control.

Step 1:
Create a website named "Loading_sequence".

empty website

Step 2:
  1. Add a Master Page named "MatserPage.master" within it by right-clicking on the website in "Solution Explorer" then choose "Add" -> "Add New Item". 

    master page
  2. Create some events in the ".cs" file of the Master Page and add a "response.write()" method with unique text for monitoring the process of execution.
    1. protected void Page_PreInit(object sender, EventArgs e)  
    2. {  
    3.    Response.Write("Master Page Pre Init event called <br/> ");  
    4. }  
    5. protected void Page_Init(object sender, EventArgs e)  
    6. {  
    7.    Response.Write("Master Page Init event called <br/> ");  
    8. }  
    9. protected void Page_Load(object sender, EventArgs e)  
    10. {  
    11.    Response.Write("Master Page Load event called <br/> ");  
    12. }  
    cs code
Step 3:
  1. Add a Contents Page named "Default.aspx" within it by the right-clicking on the website in Solution Explorer then choose "Add" -> "Add New Item" and check the "Select master page" checkbox to attach the Master Page.

    web form
  2. Select the Master Page name that you want to attach.

    select master page
  3. Create some events in the ".cs" file of the Contents Page and add a "response.write()" method with unique text by which we can monitor execution as written in the Master Page.
    1. protected void Page_PreInit(object sender, EventArgs e)  
    2. {  
    3.    Response.Write("Content Page Pre Init event called <br/> ");  
    4. }  
    5. protected void Page_Init(object sender, EventArgs e)  
    6. {  
    7.    Response.Write("Content Page Init event called <br/> ");  
    8. }  
    9. protected void Page_Load(object sender, EventArgs e)  
    10. {  
    11.    Response.Write("Content Page Load event called <br/> ");  
    12. }  
    response page
Step 4:
  1. Add a Web User Control named "WebUserControl.aspx" within it by right-clicking on the website in Solution Explorer then choose "Add" -> "Add New Item".

    web user control
  2. Create some events in the ".cs" file of the Web User Control and add a "response.write()" method with unique text for monitoring the execution as written in the Master Page and the Contents Page.
    1. protected void Page_PreInit(object sender, EventArgs e)  
    2. {  
    3.    Response.Write("User Control Pre Init event called <br/> ");  
    4. }  
    5. protected void Page_Init(object sender, EventArgs e)  
    6. {  
    7.    Response.Write("User Control Init event called <br/> ");  
    8. }  
    9. protected void Page_Load(object sender, EventArgs e)  
    10. {  
    11.    Response.Write("User Control Load event called <br/> ");  
    12. }  

    User Control Load event
Step 5:Add the Web User Control to the Contents Page then "Register Tag" and write the code to access the control by the "TagPrefix" and "TagName".

TagPrefix

Step 6:Run the website to see the output.
output

what's the page life cycle order of a control/page compared to a user contorl inside it?

Page: PreInit
Control: Init
Page: Init
Page: InitComplete
Page: PreLoad
Page: Load
Control: Load
Page: LoadComplete
Page: PreRender
Control: PreRender
Page: PreRenderComplete
Page: SaveStateComplete
Page: RenderControl
Page: Render
Control: RenderControl
Control: Unload
Control: Dispose
Page: Unload
Page: Dispose

When using  master pages, the normal page event lifecycle is a little different. Here is the actual order: Page.OnPreInit MasterPageControl.OnInit (for each control on the master page) Control.OnInit (for each contol on the page) MasterPage.OnInit Page.OnInit Page.OnInitComplete Page.LoadPageStateFromPersistenceMedium Page.LoadViewState MasterPage.LoadViewState Page.OnPreLoad Page.OnLoad MasterPage.OnLoad MasterPageControl.OnLoad (for each control on the master page) Control.OnLoad (for each control on the page) OnXXX (control event) MasterPage.OnBubbleEvent Page.OnBubbleEvent Page.OnLoadComplete Page.OnPreRender MasterPage.OnPreRender MasterPageControl.OnPreRender (for each control on the master page) Control.OnPreRender (for each control on the page) Page.OnPreRenderComplete MasterPageControl.SaveControlState (for each control on the master page) Control.SaveControlState (for each control on the page) Page.SaveViewState MasterPage.SaveViewState  Page.SavePageStateToPersistenceMedium Page.OnSaveStateComplete MasterPageControl.OnUnload (for each control on the master page) Control.OnUnload (for each control on the page) MasterPage.OnUnload Page.OnUnload - See more at: http://weblogs.asp.net/ricardoperes/asp-net-page-events-lifecycle#sthash.buRwyaHa.dpuf

No comments:

Post a Comment