Output cache in content and master page - asp.net

Two questions:
1. If I have a content page and a master page and I put this inside my content page:
<%# OutputCache ...%>
Does it cache the whole page or only the content page portion?
2. How can I apply OutputChace in the master page?
I have a master page that has a lot of content pages that uses it. I want to apply the same outputcache profile on all of them, but I dont want to go one by one and change them.
Thanks.

The whole page is cached.
Edit
You can use user controls to cache portions.
As by the comments, if you want to cache all pages that are using a specific master page, you need the following code in the master page
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetValidUntilExpires(true);
}

Content page only will be cached; unless that content page is using the master page, in which case master page also will be cached.
Unlike a content page you can't use OutputCache directive for master page. See the below links
Cache Master Page in ASP.NET
http://www.dotnetperls.com/output-cache
http://forums.asp.net/t/1236981.aspx

Related

Aspx file in two master pages

I have a page on a website that uses a master page and a child page. I would like to use the child page to create a new page on the website, but with a different master page.
Is it possible to do this without duplicating the code for the child page?
To make it clearer I have also added a link to a picture of the layouts used for the two pages.
In the picture, both children use the same code.
Edit: I have also thought about using a control for this, but I am not sure if this is the proper solution. The child page is pretty big and complex and also uses a lot of JavaScript.
When loading the child page, you can set the master page dynamically in code, in the PreInit event. Something like this:
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
That way your child page can set its master page based on whatever condition you would have in your site.

Can I use more than one master page on a sharepoint site?

I have been asked to modify a home page to a completely new design, all other pages will remain exactly the same.
I am a little green when it comes to sharepoint. I have created the masterpage, however when i choose set as default master page or set as customer masterpage it changes for the entire site. I would only like to change the home page.
The only option I have come across at this point would seem to be Detach from page layout which would not be ideal as the remainder of the site may be pushed into this new skin
Programmatically changing the masterpage for this particular page is the only option as nigel says.
You can create a custom page layout for the homepage and then set the masterpage on the pre-init as shown below:
public class MyPageLayout:PublishingLayoutPage
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
this.MasterPageFile = "~/_catalogs/masterpage/mynewmasterpage.master";
}
}
SharePoint Publishing pages inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class, which sets the master page programmatically to the site defined custom master page. There is no way to override this behaviour other than to do so through code yourself.
From your description, it sounds like you need a new page layout and not a master page.
Master page is typically used for navigation, footer, and similar outer layouts and affects all pages.
Page layout is used for specific designs and when you create a new page you can use it as template, starting point for a specific layout and typically displays your contents

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.

Loading ScriptManager only once either from Master page or Content page

I have an web application of 200 web pages and all has a single master page. Most of the content pages use AJAX controls so most of content pages has its own ScriptManager. Now I have a requirement to add a link with HoverMenuExtender control and for that I need to put ScriptManager in the Master page, but it is working only in the content pages where there is not ScriptManager.
All the other content pages which has ScriptManager throws the error Only one instance of a ScriptManager can be added to the page. I don't want to work on most of the content pages again to remove ScriptManager.
Is there any easy way to do this something like coding in Master page which decides if there is already ScriptManager already, then don't load it.
Take a look at ScriptManagerProxy. This is what I think you need.

Access body element from content page via a nested master page

All I want to do is access the <body> element from the code-behind of a content page and add a class name to it.
I have a top-level master page with the <body> element in it. Then I have a nested master page which is the master page for the content page. From the code behind of the content page I want to add a class name to the body element. That's all.
I have this in the top-level master:
<body id="bodyNode" runat="server">
I added this to the code-behind for the content page:
Master.bodyNode.Attributes.add("class", "home-page");
And I get a message that:
System.Web.UI.MasterPage' does not contain a definition for 'bodyNode
If I add this to the aspx content page:
<% # MasterType VirtualPath="~/MasterPage.master"%>
The message then changes to:
bodyNode is inaccessible due to its protection level
Please advise, I've wasted like 2 hours on what feels like something that should be really simple to do :(
once you have set runat="server" for your body node, you have to access it using the HTMLControls namespace. try this.
public void Page_Load(Object sender, EventArgs e)
{
//Inject onload and unload
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("bodyNode");
body.Attributes.Add("class", "home-page");
}
EDIT
Your problem is that you have nested master pages.
Since the "body" tag is in your top level master page, Master.FindControl() won't work, as that is looking in the nested master page.
What you need to do is use Master.Master.FindControl(), or recursively loop through your master pages, going up until Master.Master is null (as then you know you are at the top level master page) and then calling FindControl() on that.
I would add a public property to the code behind of the master page that would allow access to the body tag that is part of the master page. And then call that property from the content page.

Resources