100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET Framework

Master Pages

Understand how ASP.NET Web Forms master pages provide a shared, reusable layout that individual content pages fill in via ContentPlaceHolder regions.

Web Forms ControlsIntermediate9 min readJul 10, 2026
Analogies

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.

aspx-csharp
<%-- 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>&copy; 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

Was this page helpful?

Topics covered

#NETFramework#ClassicASPNETWebFormsWebPagesStudyNotes#MicrosoftTechnologies#MasterPages#Master#Pages#Sharing#Layout#StudyNotes#SkillVeris