Master pages in asp.net ? Execution of Master pages? - asp.net

In asp.net, i have taken one master page, 4 four child pages and one login page, which is not in a master page. My question is when a user login redirects to the page which is has master pages, is master page execute every time or only one time after login.

Master-page-engine is just part of total page life-cycle. (see for full list of taken actions there: http://blogs.thesitedoctor.co.uk/tim/2006/06/30/Complete+Lifecycle+Of+An+ASPNet+Page+And+Controls.aspx)
So short answer - yes, each time page derived from master is shown - master is executed.

A master page is executed every time a requested childpage has it as it´s parent.
So if you go to your loginpage without master and then redirects to a page with the master, the masterpage is executed once. If you later request another page with that master or the same page again doesn´t matter. The masterpage will be executed again.
Study the ASP Page lifecycle as the masterpage has some strange behaivor and acts mote like a subcontrol. You have to be a bit careful with some events.

Related

ASP.NET default.aspx page very slow to load every time

I have a simply site that uses a masterpage. I have a couple of pages that use that masterpage. However, the default.aspx page takes just over 15 seconds to load every time the page is loaded. The default.aspx page only contains a label and there is nothing in the page load event. All the other pages load very quickly and they all contain a lot of code in their page load event. They also use the same masterpage.
Does anyone know why this is happening ? I've set the page trace and I can see that the time between load begin and load end time is just over 15 seconds. The other more complex pages take under 2 seconds to load.
I've created a new master page and set the default.aspx page to use that and the problem still persists so I think it's the default.aspx page that has the problem. I'm not sure why as there is almost nothing in the default.aspx page.
Thanks,
Chris

Reload only Content Pages Inside the Masterpage

I have more than 100pages in my project & a single master page for all this. My masterpage has links to different pages. I want to change only the ContentPages( Child Pages) & my masterpage should not get reloaded.
For Example:
My Master page is : SiteMaster.master
Child Page1: Add.aspx
Child Page2: Sub.aspx
When I execute, Add.aspx comes inside the SiteMaster.master. Know, When I click the sub.aspx link inside the Add.aspx, only add.aspx page should be changed & sub.aspx must be loaded. I don't want to reload the master page.
If possible, please post some examples or links.
Your expected behavior is not exactly how master pages work. There may be ways to achieve a no postback solution in this scenario but the easiest one would be to use an <IFrame /> (which is usually frowned upon)
Master page is part of your pages. It's not loading separately.
Simple explanation:
ASP.NET engine takes your aspx and puts it inside the master page and then renders it as one page, then serves it to user.
If this is not what you want and you want that only content of your master page be loaded, then you should not use master pages at all! It's against nature of master pages. they act like skins for aspx pages.
Search for HTML IFrame tag and don't use master page.
P.S: IFrames are not used widely in this days.

multiple master pages for 1 calling page

I have 2 master pages with same (href) links on top of the page. Now these links load on the same master page when clicked on (by requirement). The problem is both these master pages have same links, so basically the same page shall be loaded in the master page.
Now what i need is when the person is on Master Page 1 and clicks on the link it should load in the same page. Whereas if the user is on Master Page 2 and clicks on the same link, i should be able to change the master page from 1 to 2 and load that in Master Page 2. Something like DirectCast.
any ideas?
You can switch the master page in the Page_PreInit event. For more of an explanation check out http://msdn.microsoft.com/en-us/library/ms178472.aspx
It would look like
void _Default_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "NewMasterPage.master";
}
I don't think I fully understand your use case, but here are some ideas:
You could track which MasterPage the user is on through Session state, and use that to remember which MasterPage to show in subsequent visits.
If you can alter the hrefs slightly, you could use a query string to indicate which MasterPage should be utilized.
Depending on your application, you could store which MasterPage should be used in your database, tied to each user.
I assume you're dynamically switching MasterPages based on some logic, so it just comes down to choosing a method to store which MasterPage should be used.

Stop Master page refreshing while navigating between pages?

I'm using Master Page in my ASP.net application, in the master page I put a ContentPlaceHolder in Update Panel to support AJAX in child pages, the question is how to stop Refreshing "master page controls" while navigating between pages?
For navigation between pages I tried to use Response.Redirect, windows.location java script with no success, shall I use the Frames or IFrames instead of Master Pages to stop Refreshing?
any suggestion to solve this issue will be highly appreciated, Thanks in advance...
If you don't want the page to refresh when switching between "pages", you will not have any good solution using master page. As others have said in different words, the master page is just a common "template" that is used by different pages. The navigation between is just like calling different pages, and of course will reload the entire page, including the master page content.
A sollution I have used with Ajax is
to have each "page" as a user
controls, and put them all in an
UpdatePanel with visible="false".
Then for navigation between "pages", switch
visibility for the user controls
to show the right "page" control.
The alternative is to use iframe.
Neither of these solutions use MasterPage.
The MasterPage concept was designed to simplify a common look before Ajax was introduced in ASP.NET. After Ajax became popular, the demand for not refreshing the entire page has been more common.
A masterpage is nothing more than extending your "normal" page with (most of the time) the default layout of your application. The master page and the contentplaceholders are rendered as a full html page. When you navigate between pages it is the normal behavior that your whole page refreshes. This is how the web works.
Working with an iframe could solve your problem. However that has some other side effects:
The whole masterpage isn't useful anymore. The content around your iframe is the "masterpage".
With a masterpage you actually browse to another url, you also see in the url bar of your browser. When you work with an iframe you navigate within the iframe to another page. The url in your browser will stay the same. When the user of your application hits the refresh button it always starts again at the default page you assigned to your iframe in the html. Of course there are some workarounds
Anyway. It really depends on your application. There are multiple solutions to work around the refresh behavior.
Having a structure like the one you've explained:
Master
Child page 1
Child page 2
...
Then you cannot prevent the page from refreshing when you switch from page 1 to page 2 etc. for you have a single "page" entity (master content + selected page content) when it's rendered to the browser.
If you want to switch betweent different app views inside the very same page (so to prevent a complete page refresh) you could use a single page (the Master becomes quite useless) with an updatePanel in which you load the different views.
You can also use iFrames, but if you have to handle any type of communication between different parts of the page (some of which are inside iFrames) I would personally advice not to use them.

Problem with Master pages event order

I have a simple setup with the master page housing some controls used by all child pages.
I found when moving to new pages the master page page loads event fires as a non post back and read the solution was to store it's current values somewhere for retrieval. Ok all done.
The child page uses these values to run a report. When I switch to a new report, all is well. If I change the values in the master page the master page and the sub page load events fire.
The load event for the sub page fires first, picks up the values from the master page which are still the old values and then finally the master page events fire and all the new values are stored. The report hasn't changed as it still ran from the old values.
I can't really see a way around this. All you ever hear is that master pages are a saving grace but I swear i've never jumped through so many hoops to get a page to load correctly.
And now this!
Anyone see a plan to resolve it?
Populating the controls during the Masterpage's Init will solve your issue from the sounds of it.
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
An alternate approach would be to have a public sub in the content page(s) that you can call from the masterpage during load which in effect acts as an alternate to the page load event.
A slightly more indepth look at the page lifecycle when using masterpages:
http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

Resources