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

Sheonarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 7205 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

Introduction

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 ITFunda.com - 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).

MASTERPAGE.MASTER

 

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

 

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

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

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

</asp:ContentPlaceHolder>

</head>

<body>

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

<div>

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

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

 

</asp:ContentPlaceHolder>

</div>

</form>

</body>

</html>

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

CREATING CHILD MASTER PAGE

CHILDMASTERPAGE.MASTER

<%@ 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>

<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">

 

</asp:ContentPlaceHolder>

 

</asp:Content>

 

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.

ASPX 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>

</asp:Content>

 

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.

OUTPUT

 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

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)