Web Pages Tutor...login
Web Pages Tutorial
author:php.cn  update time:2022-04-11 14:20:28

Web Forms Master Page



Master pages provide templates for other pages on your website.


Master Page

Master page allows you to create consistent appearance and behavior for all pages (or groups of pages) in your web application.

Master pages provide templates for other pages, with shared layout and functionality. Master pages define placeholders for content that can be overridden by content pages. The output is a 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 pages to produce an output that combines the master page layout and content page content.


Master page example

<%@ Master %>

<html>
<body>
<h1>Standard Header From Masterpage</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>

The master page above is an ordinary HTML template page designed for other pages.

@ Master directive defines it as a master page.

The master page contains placeholder tags <asp:ContentPlaceHolder> for separate content.

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

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

lamp.gif Note: Master pages can also contain code, allowing 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 above content page is one of the independent content pages in the site.

@ Page directive defines it as a standard content page.

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

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

When the user requests this page, ASP.NET will merge the master page and the content page.

Click here to display mypage1.aspx

lamp.gifNote: The content text must be inside the <asp:Content> tag. Content text outside of tags is not allowed.


Content page with controls

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

<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>W3CSchool</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 a .NET control into the content page, just like inserting it into an ordinary page.

Click here to display mypage2.aspx


php.cn