Programmatically define a nested masterpage's master, Is it possible? - asp.net

Currently I have a site that is set up using a masterpage and a nested master page. The master page setups up the header and footer info. The nested masterpage is used once logged into the site.
The issue I have is that I want to programmatically load a different masterpage to define different header and foot info.
I don't think I can use the OnPreInit() in each content class to set a different masterpage. I don't think I can do this because each Content page uses the Nested Masterpage.
What I would like to do is programmatically set which masterpage is called in the NestedMaster.
Any ideas?
I saw this blog posted on another MasterPage question. Before trying this route I wanted to see if anyone else has experienced this.
Thanks
EDIT:
In the CS page:
public class AdminBasePage : BasePage
{
protected override void SetMasterPageFile()
{
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
}
}

Look here:
Nested Master Pages

Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
(Thanks for the answer Ken and Brad, however I read most of that long tutorial before spotting the simple one line of code had been added to your question, so am making it more obvious by adding it as an answer here and will edit your post. Cheers)

Related

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

Accessing the ControlsPlace Holders from Master Page

Hello I have been stuck with this situation for quite some time? Looking for some help here?
I have defined Nested Master Page (1 Default for WebApp and other Custom Master Page referencing the Default One)
The following is the code snipet from content page(say content.aspx) consuming my Custom Master Page
in aspx source code I have included:
%# MasterType VirtualPath="~/NestedMasterPage1.master" %>
and in code behind file ie. custom.aspx.cs (This is where the problem is):
ContentPlaceHolder masterContentPlaceHolder = (ContentPlaceHolder)Page.Master.Master.FindControl("MainContent"); //works well
ContentPlaceHolder nestedContentPlaceHolderHeading = (ContentPlaceHolder)masterContentPlaceHolder.FindControl("NestedMasterHeading"); //works well
Label NewsHeadLines = (Label)nestedContentPlaceHolderHeading.FindControl("lblSubSectionHeader"); //returns null?? The Control ID is all checked and is the same in the Nested Master Page.
**NewsHeadLines.Text = "Testing";** //System.NullReferenceException:
Object reference not set to an instance of an object.
Is it happening because I have nested my Control(here the label) in HTML Tables,rows and columns? Please advise? And I have double checked the control names or IDs.
I believe you might be missing one or two nested containers. Try this; instead of you trying to figure out the nested containers try having a common programming logic to get them. Have a look at Rick Strahl's blog post

inheriting the .aspx pages?

Can we inherit the aspx page into another aspx page. If yes how can we do that, Thank you.
I think there are two things you should consider here::
1.Create a new base page type, and have your codebehind classes inherit from that, e.g.:
public abstract class MyPageBase : System.Web.UI.Page
{
// Implement custom shared logic here.
}
2.Move some of your page control logic into partial controls that you can inject into other pages.
I don't think so. You can however, create a master page and use it in the pages you want. That is the way "inheritance" of aspx pages works.
Master page tutorials:
http://www.asp.net/master-pages/tutorials
You can inherit the code (i.e. the classes derived from System.Web.UI.Page), but you cannot inherit the markup.
For markup "inheritance", use ASP.Net mechanisms such as User Controls (ascx) or MasterPages, as other responses suggested, or create controls dynamically.
I am not entirely sure that I understand your question correctly, but you surely can inherit a ASPX page. In your code behind file for a page you have a class declaration where the page inherits from System.Web.UI.Page. If you want to use a different base page, you could simply make another base class that inherits from System.Web.UI.Page and change the code behind defined classes inherit from you new base page.
The problem will of course be to make an appropriate layout shared elements and if that is your main concern you are probably better of using master pages.

ASP.NET MVC 1.0 Nested Masterpages

I have a problem with my master pages. They have such inheritance order:
MainMaster1.Master can be nested by Nested1.Master, Nested2.Master, etc.
At the same time MainMaster can be duplicated and have working copies like MainMaster2, MainMaster3, etc.
Advise please how can I dynamically change the MasterPageFile of my Nested1, Nested2, etc. pages so that they can easily switch between MainMasters if needed?
I tried to treat the problem in Page_PreInit of the nested masters but couldn't get it entering this event handler. I also tried to change the masters in "protected void OnPreInit" of nested ones but result was the same.
Thanks,
Roman.
See the following article for several possible techniques you can use:
Dynamically Switching between Master Pages in ASP.NET MVC http://www.codeofrob.com/archive/2009/11/01/dynamically-switching-between-master-pages-in-asp.net-mvc.aspx
I think that's still what he meant, although it doesn't directly answer the question.
Page has a property called MasterPageFile which is used in the above article
Page also has a property called MasterPage
On MasterPage, there is a property called MasterPageFile (As in the above article)
MasterPage also has a property called MasterPage
While the example on that page covers with changing the master page on the page directly, you can do very much similar to the master page itself by recursing up through the master pages to find and change the one you want.
I hope that helps.

Masterpage and Content pages inheriting from a single class

I am trying to create a general class, in which all my ASP.Net pages inherit from so I can share functions across multiple pages.
To do this I would create a new class which inherits from System.Web.UI.Page (the content pages need to inherit this), and then my content pages would inherit the newly create class.
My problem is that the Masterpage inherits from System.Web.UI.Masterpage.
How can I set up my project so both content pages and Masterpage and use functions from the general class?
Please don't hesitate to ask if I am unclear!
Thanks!
E
First, not sure why you'd want to do this. By their function Master Pages should mostly have functions that your Pages shouldn't be concerned with and visa versa. And if you just need some common functionality that isn't page dependent you can just create a static class (much like Math) or a helper class of some kind that you can implement in MasterPage and Page custom base classes.
But your only real option is to create two custom base classes. One that inherits MasterPage and the other from Page. Both will need to implement an interface ICommon which you create. Then create another static class that you can proxy all the functions to.
Yucky solution but it's the only one I can think of.
EDIT
Here's a better solution
public class Helper
{
public static int getUserID(...)
{
// ... Code to get User ID
}
}
In your masterpages and pages use
int UserID = Helper.getUserID(...);
I don't think you can do this, the MasterPage inherits UserControl and Page inherits TemplateControl. Like Spencer said, I would just create a Helper/Utility class.
I know It's too late, but just want to share some one might come across the same.
This is a sample code using Extension functions:
public static void Commonfunction(this TemplateControl ctl)
{
// Your Code here
}
Call this function as
this.Commonfunction();
in any Page or MasterPage.
Although the question is 5 years old, I wanted to share another option for future readers.
From what you explained, I suppose that what you call MasterPage is like a frame which loads the content page on a side of the screen, for example.
If I'm right, you can create a BaseMasterPage class with your functionality and then create a MasterPage for your content pages (which you could also use to place some of the code from your current pages). Next step is obviously to make both your current MasterPage and, let's say, ContentMasterPage inherit from this BaseMasterPage.
So this way you can end up having, for example, a LoggedUser property in both your frame page and content pages.

Resources