Multiple Master Pages - Inheritance?

R

Rich

Hi,

I want to use 2 master pages, one for the main part of the site, the other
for the admin pages.

They are quite similar, with many shared elements.

Ideally I would like to have a parent master page, which both of these child
master pages are based on, and in turn the final pages are based on one of
these child master pages.

This would form an inheritance hierarchy, so any change could be made to the
parent master page, which would affect both children, or to each child
individually.

Is this possible?

Cheers
Rich.
 
J

Joe

Hi Rich,

You can do what you want in ASP.NET 2.0.
Create a master page say Site1.master
Now create another master page say Child.master and set it's master to
Site1.master.

Now the problem you're going to get is that VS doesn't support design dev
with embedded master pages. To work around this you'll need to do the
following:

Create a new class like this:
public class BasePage : System.Web.UI.Page
{
private string runtimeMasterPageFile;

public string RuntimeMasterPageFile
{
get
{
return runtimeMasterPageFile;
}
set
{
runtimeMasterPageFile = value;
}
}

protected override void OnPreInit(EventArgs e)
{
if (runtimeMasterPageFile != null)
{
this.MasterPageFile = runtimeMasterPageFile;
}

base.OnPreInit(e);
}
}

Make each of your child pages (content pages) inherit from this base class.
In the aspx page of each child page add the following attrbiute to the top:
RuntimeMasterPageFile="~/Child.master" and make sure that
MasterPageFile="~/Site1.master".

What this does is use the Site1.master at design time but changes it to use
Child.master at runtime.

You still will not be able to use the design tools to create your
Child.master but will be able to use the designer for all content pages.

Note: I found this code on the web but cannot remember where. I would like
to give credit to the person who wrote (if I knew who it was).

Hope this helps.
-Joe
 
C

Cowboy \(Gregory A. Beamer\)

You can do this, but you lose the IDE support for the pages, so you have to
code blind. Best bet is to complete the pages, as you wish, and refactor the
common bits and then test.
 
R

Rich

Thanks for that, it's a nice solution.

Still wish it wasn't necessary...

At the moment I am just using some conditionals in the template. If that
gets too ugly, I will implement this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top