views:

332

answers:

1

Is it possible to make one master page simply include another master page?

I have three master pages, which have converged in content, and I want to make 2 of them point to the third, so that the content is not replicated, but leaving them in so that they can change easily in the future if they have to.

+2  A: 

Yes, what you need is called nested master pages. Just set the MasterPageFile in the <%@ Master %> directive of child master pages to the parent one.

Main.Master:

<%@ Master Language="C#" %>

.... shared content ....
<asp:ContentPlaceHolder ID="C" runat="server" />

First.Master:

<% Master Language="C#" MasterPageFile="Main.Master" %>

<asp:Content runat="server" ContentPlaceHolderID="C">
   .... Some content ....
   <asp:ContentPlaceHolder ID="AnotherPlaceholder" runat="server" />
</asp:Content>

Second.Master:

<% Master Language="C#" MasterPageFile="Main.Master" %>

<asp:Content runat="server" ContentPlaceHolderID="C">
   .... Some other content ....
   <asp:ContentPlaceHolder ID="AnotherPlaceholder" runat="server" />
</asp:Content>
Mehrdad Afshari
Should the ContentPlaceHolder's defined in the Main.Master page, be accesible by content pages that use First or Second.Master as their master page?
AlexH
@AlexH: It won't be directly accessible. If you want to propagate it to the page: <asp:Content ContentPlaceHolderID="ID" runat="server><asp:ContentPlaceHolder runat="server" ID="ID /></asp:ContentPlaceHolder>. A page will only see ContentPlaceHolders specified directly in its master page.
Mehrdad Afshari