Latest web development tutorials

ASP.NET master pages

Master pages provide templates for other pages of your site.


Master pages

Master pages allow you to your web application, all the pages (or group of pages) to create a consistent appearance and behavior.

Master pages provide templates for other pages, with shared layout and functionality. Master page to define the contents of the page content can be covered by a placeholder. The output is a combination of master pages and content pages.

Content pages contains the content you want to display.

When users request the content page, ASP.NET pages will be combined to produce a combination of the master page layout and content pages of output.


Examples of master pages

<%@ Master %>

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

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

@ Master directive defines it as a master page.

Master page contains a placeholder for individual tag<asp: ContentPlaceHolder>.

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

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

lamp NOTE: The master page can also contain code, allowing dynamic content.


Examples of content pages

<%@ 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 independent of the site content in a page.

@ Page directive defines it as a standard content page.

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

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

When the user requests this page, ASP.NET master page will be merged with the page content.

Click here to show mypage1.aspx

lamp Note: The contents of the text must be located in <asp: Content> tag inside. Content of the text-label is not allowed.


With control of the content page

<%@ 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 .NET controls to insert content pages, like the insertion of a normal page.

Click here to show mypage2.aspx