Aspx file in two master pages - asp.net

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.

Related

updating master page image from content page

Have made several attempts to update an image in a master page div when switching between content pages. I first tried creating a master page property that I could update from the content page's Page Load event by declaring "MasterType VirtualPath=", but since the master is already loaded by this time it wasn't going to work. It did work when I set the ImageUrl in the master page's page load event (if !Page.IsPostBack then set the image's url attrib), so I know it can work, but I need the image to change for every content page I visit.
Then I tried using the master's menu button click events to set the ImageUrl before loading the content page but this also had no effect. I saw a thread suggesting the use of an UpdatePanel to hold the image, so I may try that next. What's the best way to do this..??
I wouldn't be surprised if a better way is to have the image in a content div instead, and updating that from the master. Any suggestions or links would be most welcome. I can post code if anyone would like to have a look. thanks.
I don't know why you found it difficult. There are many ways to do this but I'll only show one. I just tested this and it worked. How?
In your master page, define your image and add runat="server"
<img src="put your default image.jpg" runat="server" id="changingImage" />
In your content pages, do this
protected void Page_Load(object sender, EventArgs e)
{
HtmlImage img = Master.FindControl("changingImage") as HtmlImage;
img.Src = "~/images/imageForContentPage1.jpg"; //replace this image based on your criteria
}
Possible exception is Null Reference when the name of the image control specified in .FindControl could not be found. Make sure it's exactly as you named it in the Master Page. And to prevent a Null Reference Exception, wrap a check around
if(img != null)
{
img.Src = "~/images/imageForContentPage1.jpg";
}

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

Checking Request.ServerVariables["HTTP_REFERER"] on every request

I am using this code to check if the request came from a page , if not then redirect somewhere.
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
It is working , I don't know whether it is perfect the solution.However I am checking this on load event of one of my page.How can I make it check on every request.Should I check this for all my pages.Also it is a good approach.Can anybody point me in the right direction.Any suggestion is welcome.
If you have logic that you would like to be run on the OnLoad of a bunch of your pages. You should probably create a BasePage that derives from Page and have the logic inside. Then all the pages you want that logic in can derive from BasePage instead of the regular Page.
Another approach can be using Master Pages
Note: After reading OPs additional comments. One thing to look out for when using a Master Page is that the Master Page's Page_Load event happens AFTER the Content Page's Page_Load event.
In other words the lifecycle is like this:
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
If your response.redirect moves the user to another page with the same master page (and same "validation" check) you might find yourself in an endless loop :)
If you have lot of pages, with these kind of common codes, than one possible solution is creating your own MyPage class as a child of the standard Page class. In your MyPage you can use something like:
Page_Load(object sender, EventArgs e)
{
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
base.Page_Load(sender, e);
}
Then any of your pages can inherit from this own MyPage class instead of the .NET's standard one.
In this way the common code reside in one place. In case of any change you have to modify that only there.
Or another possibility, you can consider using Master Pages.

Output cache in content and master page

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

How to change the Master Page dynamically

I want to assign one master page dynamically for a pure aspx file, Anybody can tell me, how to do this?
You can override OnPreInit in your default.aspx.cs and set the master page based on some value in your querystring. Something like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.QueryString["Master"] == "Simple")
MasterPageFile = "~/Masterpages/Simple.Master";
}
EDIT: the cause of your error message might be covered by this question.
I Left the ContentPlaceholder to add on it.. Actually , I tried to assign master page without using ContentPlaceHolder.. Now, I realised that, atleast one ContentPlaceholder should be there temporarily, even though we will change the master page dynamically...

Resources