Master pages in ASP.NET – Nested master pages

Sometimes you want to use a generic layout to the entire website, and in 2 “sub sites” like forum and admin you want to have another master file to dictate layout for that specific section.

What you need to do is create a parent master file and two sub master files. Like the image below.

image

The Parent.Master file have two ContentPlaceHolder controls.

<asp:ContentPlaceHolder ID=”HeadContent” runat=”server”></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID=”MainContent” runat=”server”/>

This two must exist in the child master files also.

You also have to reference the Parent.Master file in both Admin.Master and Forum.Master like the image below (showing Forum.Master)

image

Now when all master pages is created and configured all you need to do to use them is to add a Web Form using Master Page, click Add. And select the master page you want.

image

image

The @ Page directives will look like the following line of code, for pages using the Admin.Master.

<%@ Page Title=”" Language=”C#” MasterPageFile=”~/Admin.Master” AutoEventWireup=”true” CodeBehind=”Admin.aspx.cs” Inherits=”Prototype_Master_Pages.Admin1″ %>

And to round up this post a image of the actual ASPX page using the Admin.Master file.

image

The text Admin is rendered from the Admin.Master.

Follow

Get every new post delivered to your Inbox.