Using ASP.Net Master Pages and Content Pages
Master pages allow the same content to be displayed on multiple pages, typically they are used for headers, footers and sidebars.
- What is ASP.Net?
- ASP.Net for PHP Developers
- Creating a Simple ASP.Net Page
- ASP.Net Website Navigation Using a SiteMap
- Using ASP.Net Master Pages and Content Pages
- Validating Input using ASP.Net
- Tracing and Remote Debug in ASP.Net
- Creating Custom Error Pages with ASP.Net
- Managing Session State in ASP.Net
- Using Themes and Skins in ASP.Net
- Creating User Controls in ASP.Net
- Difference between ASP.Net User and Custom Server Controls
- Creating Fully Themable Websites with ASP.Net
- Ultimate Guide to the Web.Config File
- Adding StyleSheets to ASP.Net via C#
- Extending the Web Sitemap Xml Document
Adverts Blocked Please disable AdBlocking software and allow me to set cookies so that I can continue providing free content and services.
Master pages are created from the new item screen and should be given a meaningful name. One master page will contain the header, footer, any sidebars and a placeholder for the content. When creating a new asp.net web form you are given the option to select a master page. This will link to the content page with the master page.
You should notice that the master page contains all the HTML header tags as well as the body tag. In between the body tag is a ContentPlaceHolder. When a request for the content page is made it is merged with the master page and the ContentPlaceHolder object is replaced with the content within the content page. Master pages cannot be invoked through a browser as themselves, only through a content page.
My basic master page looks like this:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div class="header"><h1>This is my Website Header</h1></div> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> <div class="footer"><h1>This is my Website Footer</h1></div> </form> </body> </html>
Notice that the default page title is "Untitled Page". Should be changed to your site or you can override the title from the content page (see below).
The content page in my example sets the title attribute and writes out a paragraph.
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="My Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <p>This is my content</p> </asp:Content>
The result when requesting default2.aspx from the server is:
<div class="header"><h1>This is my Website Header</h1></div> <div> <p>This is my content</p> </div> <div class="footer"><h1>This is my Website Footer</h1></div>
In the content page, I have set the title to "My Page" and this is intercepted by ASP.Net and the default Untitled Page is overwritten.
In the next tutorial, we will look at site navigation elements that can be added to master pages to create useful headers and footers.
Last updated on: Friday 23rd June 2017
There are no comments for this post. Be the first!