Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Master Pages

ASP.NET Web Forms - Master Pages


A master page provides a template for the other pages of your website.


Master Page

A master page allows you to create a consistent look and behavior for all pages (or groups of pages) in your web application.

A master page provides a template for other pages, with shared layout and functionality. The master page defines placeholders that can be overridden by content pages. The output is a combination of the master page and the content page.

A content page contains the content you want to display.

When a user requests a content page, ASP.NET merges the page to produce output that combines the master page layout with the content page content.


Master Page Example

<%@ Master %>

Standard Header From Masterpage

The master page above is a plain HTML template page designed for other pages.

@ Master The directive defines it as a master page.

The master page contains placeholder tags for separate content asp:ContentPlaceHolder。

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

This master page is saved as "master1.master"。

lamp Note: A master page can also contain code, allowing dynamic content.


Content Page Example

<%@ Page MasterPageFile="master1.master" %>

Individual Content

Paragraph 1

Paragraph 2

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

@ Page The directive defines it as a standard content page.

The content page contains the content tag asp:Content, which references the master page (ContentPlaceHolderId="CPH1").

This 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

lampNote: Content text must be located within the asp:Content tag. Content text outside the tag is not allowed.


Content Page with Controls

<%@ Page MasterPageFile="master1.master" %>

RUNOOB

The content page above demonstrates how to insert .NET controls into a content page, just as you would insert them into an ordinary page.

Click here to display mypage2.aspx