Sharing Layout with Master Pages
A master page, saved with a .master extension, defines the shared chrome of a site — header, navigation, footer, and CSS/script references — and declares one or more asp:ContentPlaceHolder controls marking the regions that individual content pages are allowed to fill. A content page then sets its @ Page directive's MasterPageFile attribute and wraps its own markup in asp:Content controls, each one targeting a specific ContentPlaceHolderID, so the final rendered page is the master's layout with the content page's fragments merged into the matching placeholders.
Cricket analogy: A master page is like a fixed broadcast template used for every match on a channel — the scoreboard bug, sponsor banners, and camera framing stay constant while only the live match feed slot changes each game.
How ContentPlaceHolder Merging Works
At runtime, ASP.NET parses the content page, locates its MasterPageFile, and merges the two control trees: everything inside an asp:Content block is injected into the ContentPlaceHolder with the matching ContentPlaceHolderID, while any markup in the master page outside a placeholder is rendered as-is on every page that uses it. A content page cannot place markup outside of asp:Content tags — anything outside those tags in a page that specifies MasterPageFile is simply ignored — and a master page can itself reference another master page to build nested layouts for sections of a site that need a shared sub-layout, such as an admin area within a public site.
Cricket analogy: ContentPlaceHolder merging is like a TV production switching the live match feed into a fixed broadcast frame — the frame around it (score bug, sponsor logos) never changes, only the feed content does.
<%-- Site.master --%>
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>My Site</title>
<link rel="stylesheet" href="/Content/site.css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<header>My Company</header>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<p>Default content if the page doesn't override it.</p>
</asp:ContentPlaceHolder>
</form>
<footer>© 2026 My Company</footer>
</body>
</html>
<%-- Products.aspx --%>
<%@ Page Title="Products" Language="C#" MasterPageFile="~/Site.master"
CodeFile="Products.aspx.cs" Inherits="Products" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h1>Our Products</h1>
<asp:GridView ID="gvProducts" runat="server" />
</asp:Content>Setting the Master Page Dynamically
Rather than hard-coding MasterPageFile in the @ Page directive, a site can select a master page at runtime in the PreInit event, the earliest point in the page lifecycle where the master can still be changed: Page.MasterPageFile = "~/AltSite.master" set inside Page_PreInit lets an application swap layouts per user, per theme, or per section of the site (for example, a slimmer master page for a mobile-optimized area). Content pages can also expose strongly typed properties or public members that the master page reads, and, in the other direction, a content page can access members on its master via the Page.Master property cast to the specific master page type, enabling two-way communication between page and layout.
Cricket analogy: Switching MasterPageFile dynamically per user is like a broadcaster swapping the entire graphics package between the men's and women's IPL coverage while reusing the same underlying production pipeline.
MasterPageFile can only be changed during or before Page_PreInit. Setting it any later, such as in Page_Load, throws an InvalidOperationException because the control tree has already started being built against the original master page.
- A master page (.master) defines shared layout and declares ContentPlaceHolder regions for content pages to fill.
- Content pages set MasterPageFile in the @ Page directive and wrap their markup in asp:Content controls.
- Each asp:Content targets a specific ContentPlaceHolderID; markup outside asp:Content tags is ignored.
- Master pages can be nested to give a sub-section of a site its own shared sub-layout.
- MasterPageFile can be set dynamically, but only in or before the Page_PreInit event.
- Page.Master, cast to the specific master page type, lets a content page call members exposed by the master.
- Master pages centralize header, footer, navigation, and shared CSS/script references in one place.
Practice what you learned
1. Which control in a master page marks a region that content pages are allowed to fill?
2. Which attribute on the @ Page directive links a content page to its master page?
3. What happens to markup placed outside of asp:Content tags in a page that specifies MasterPageFile?
4. In which page lifecycle event must MasterPageFile be set if changed dynamically in code?
5. What does Page.Master, cast to the specific master page type, allow a content page to do?
Was this page helpful?
You May Also Like
User Controls
Learn how ASP.NET Web Forms user controls (.ascx) let you package reusable markup and code-behind logic into components shared across pages.
Navigation Controls
See how ASP.NET Web Forms handles site navigation through the Web.sitemap file, SiteMapPath, Menu, and TreeView controls, plus programmatic redirection.
Data Controls: GridView and Repeater
Learn how ASP.NET Web Forms renders bound data using the feature-rich GridView control and the fully customizable Repeater control.
The Page Lifecycle
A walkthrough of the ASP.NET Web Forms page lifecycle — Init, Load, events, PreRender, and Unload — and why the order determines where your code belongs.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics