Which page is called first in asp.net c#? Web form or Master page?

Today in Engineer's World, we are discussing a C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imagination hunt to see latest Blogs


Which page is called first in asp.net c#? Web form or Master page? 

Second Scenario: We take the same pages (master and web form), we are working with case one and write the below code in the page load for both pages.

Code that we write on master page

Practical Implementation:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Master Page Loaded<br/>" + "<br/>");
    }
}

And, Code that we write on web form page

Practical Implementation:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Content Page Loaded" + "<br/>");
    }
}

Now, the question arises
Which page load is called first? Is it the Default.aspx.cs or MasterPage.master.cs?
Let’s run and check.



OUTPUT:

Reason explanation: As per the event sequence for pages – Web Form page control load before master page control. That’s why Content Page loads first than Master Page. 


Related Questions:


Q-1 How can I first execute page load of the master page?
Ans. It’s not possible to change the order of the events.
But, there’s a way by which you can place the desired result. Placing the master page code within the Page_Init event instead of Page_Load has the desired effect to firing before the child page Page_Load event.

Q-2 Why page load of web form load first then the master page load event?
Ans. As per the event sequence for pages – web form page control load before master page control. That’s why web form page loads first then master page.

Click imagination hunt to see latest blogs

Keep learning and sharing…

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions