By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Multiple Master Pages - Inheritance?

P: n/a
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.
Feb 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Rich" <r@r.rwrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au...
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.

Feb 18 '07 #2

P: n/a
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.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

********************************************
Think outside the box!
********************************************
"Rich" <r@r.rwrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au...
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.
Feb 18 '07 #3

P: n/a
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.

"Joe" <jb*******@noemail.noemailwrote in message
news:Op**************@TK2MSFTNGP05.phx.gbl...
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
"Rich" <r@r.rwrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au...
>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.


Feb 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.