Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 31 December 2015

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…

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...