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


Similar Articles