I've got a "Master" Masterpage, and I've got 2 "Sub" Masterpages. Those 2 subs have their masterpage property set to the "Master" Masterpage. All good so far.
Well, I was told today to also make Sub1 and Sub2 actually "inherit" from the Master Masterpage. I thought they were being imprecise with their language, but no, she wanted me to change the Class definition to
public partial class Sub2 : TheMaster
(those aren't the names).
So, now I've got Sub1 and Sub2 that are master pages, and have a master page of TheMaster, and ALSO they are of the class of "TheMaster"
I'm quite confused by this, and not sure what kinds of effects this will have. It seems to compile and run fine right now, but we are just starting this 6 month project today, and I don't want to get to month 5 and find out that we have a major design flaw.
Can anyone tell me that this is totally fine, or that we are completely messed up?
I'm scared...hold me.
EDIT: Clarification -- the reason she wanted me to also inherit from the master master page is that we set some things in that page, and we want them available to the sub master pages. Things like the current node in the sitemap that the rendered page is, some user account stuff, and much more.
It is indeed possible to have nested master pages - see http://msdn.microsoft.com/en-us/library/x2b3ktt7.aspx for a reference.
EDIT as per comment
I don't believe your sub master page should be inheriting from the main master page in the code behind.
Each master page, including the sub master pages should inherit directly from MasterPage, i.e. public partial class Sub1 : System.Web.UI.MasterPage
.
Only the ASP markup of the sub master page should be referencing the main master page, i.e. <%@ Master Language="C#" MasterPageFile="~/TheMaster.master" ... />
If you add your sub master page to the project via the VS UI, selecting TheMaster.master as the master page then you will see that this how things are set up. Master page usage is designed to be only via content (markup), and not via class inheritance.
I just ran into this, and wanted to add specific problems you'll run into when inheriting from master pages you're also nesting. For example, the following code in BaseMaster.master.cs:
//BaseMaster.master.cs
public String FooLabelText
{
get { return FooLabel.Text; }
set { FooLabel.Text = value; }
}
when called from your inherited page will throw a NullReferenceException
. FooLabel (a label in your BaseMaster.master
file) doesn't exist in the nested master page (SubMaster.master
) so if you access that property it will break.
You could get around this by overridding the property in your subclass and re-directing it to the master page you're nested in, but this really breaks the design philosophy of .NET master pages:
//SubMaster.master.cs inherits from BaseMaster
new public String FooLabelText
{
get { return (Master as BaseMaster).FooLabel.Text; }
set { (Master as BaseMaster).FooLabel.Text = value; }
}
Note that I'm not using base.FooLabel
, but Master.FooLabel
, which is really pointing to a different object. At best, this is a hack, and in many cases it's going to create more work and duplication.
One alternative would be to create a virtual master page base class with the desired application functionality, and have each master page inherit from this class. I can only see very limited advantages to using this over using a virtual page base class, and many downsides. It's probably best to use master pages for what they're best at, reducing replication in your markup, and put the rest of the required functionality in your page classes.
Basically what you have done is setup the page so that the class inherits all of the items from the specified base. The hookup of the "Masterpage" property, creates a weak association, and renders content inside of a specific structure. The class inheriting from the base moves the logic forward, into a manner where rather than just having it execute, you can override it.
Now, after thinking about it, you most likely do not want the MasterPage property set....but just the class inheritance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With