How to nest more than one master page in ASP.NET?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 11263 red flag
Rating: 4 out of 5  
 3 vote(s)

In this article, we shall learn how to nest more than one master page in ASP.NET


Master page allows us to share the common content as well as layout to multiple pages. The best example is to show same header and footer and the same layout across different pages of the website. For example on - an ASP.NET online training website you can notice that all pages have same header and footer but in between content is different. The layout in which header and footer exists is the master page (.master) and rest content comes from the content page (.aspx page).

In this article, let us learn how to nest more than one master page in ASP.NET. To demonstrate this I have created a demo page.

Get hundreds of .NET Tips and Tricks and online ASP.NET training here.

Below is my first parent master page (MasterPage.master).



<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="NestedMasterPage_MasterPage" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



<html xmlns="">

<head runat="server">


<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">




<form id="form1" runat="server">


<p style="color: Red;">This text is from parent master page</p>

<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">







In this parent master page we have written a paragraph text in the red color.



<%@ Master Language="C#" MasterPageFile="~/NestedMasterPage/MasterPage.master" AutoEventWireup="true" CodeFile="ChildMasterPage.master.cs" Inherits="NestedMasterPage_ChildMasterPage" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<p style="color: Blue;">This text is from Child master page</p>


<asp:ContentPlaceHolder ID="ChildContentPlaceHolder" runat="server">






In this child master page we have written a text in the blue color. Notice that while creating this master page, we had checked the Select Master Page checkbox on the dialogue box and then selected the Parent master page.


<%@ Page Title="Home" Language="C#"

MasterPageFile="~/NestedMasterPage/ChildMasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="NestedMasterPage_Default" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ChildContentPlaceHolder" runat="Server">


<p style="color: Green;">This text is from content page.</p>



In the above steps we have created a master page as usual (MasterPage.master). Then we created a child master page (ChildMasterPage.master) by using parent master page (MasterPage.master). On child master page we have placed ContentPlaceHolder as per our requirement.

Then we have created a content page (default.aspx) page by using the ChildMasterPage.master.

In this content page we have written a text in the green color. When we run the page and the page renders, it shows the text content from the parent master page, child master page as well as content page as shown in the picture below.


 In above example we have seen that we have nested two master pages and the content page is getting content and layout (in this demo I have not created any layout but in real time you can) both from the parent master page and the child master page.

Hope this article would be useful. Thanks for reading!

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)