ASP.NET 2.0 - Master Page (Master Pages)

Master Page (Master Pages) provides templates for other pages within the website.

Master Page (Master Pages)

Master Page enables you to create a consistent look and feel for all pages (or page groups) in a web application.

Master Page provides templates for other pages, with shared layout and functionality. Master Page defines placeholders that can be overridden by content pages. The output is the combination of Master Page and content page.

The content page contains the content you want to display.

When a user requests a content page, ASP.NET merges the page to generate output, which combines the layout of the Master Page and the content of the content page.

Master Page Example:

<%@ Master %>
<html>
<body>
<h1>Standard Header For All Pages</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>

A Master Page is a common HTML template page designed for other pages.

@ Master directiveDefine it as a master page.

This master page contains a placeholder tag for individual content. <asp:ContentPlaceHolder>.

id="CPH1" The attribute identifies the placeholder, allowing multiple placeholders in the same master page.

The master page is saved as "master1.master".

Note:This master page can also contain code, allowing for dynamic content.

Content Page Example:

<%@ Page MasterPageFile="master1.master" %>
<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>Individual Content</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</asp:Content>

The content page above is one of the independent content pages.

@ Page Directivedefining it as a standard content page.

This content page contains a content tag<asp:Content>, which tag refers to the master page (ContentPlaceHolderId="CPH1").

The content page is saved as "mypage1.aspx".

When the user requests this page, ASP.NET merges the master page with the content page.

Click here to display mypage1.aspx.

Note:Content text must be located within the <asp:Content> tag. Text outside this tag is not allowed.

Content Page with Controls

<%@ Page MasterPageFile="master1.master" %>
<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>W3School</h2>
<form runat="server">
<asp:TextBox id="textbox1" runat="server" />
<asp:Button id="button1" runat="server" text="Button" />
</form>
</asp:Content>

The content page above demonstrates how to insert .NET controls into a content page, just like inserting them into a regular page.

Click here to display mypage2.aspx.