Web Design that taps into the haromny and vision of your dreams.

Using ASP.Net Master Pages and Content Pages

Using ASP.Net Master Pages and how they work with Content Pages

Written By on in C#

523 words, estimated reading time 3 minutes.

Master pages allow the same content to be displayed on multiple pages, typically they are used for headers, footers and sidebars.

Active Server Pages (ASP.Net) Series
  1. What is ASP.Net?
  2. ASP.Net for PHP Developers
  3. Creating a Simple ASP.Net Page
  4. ASP.Net Website Navigation Using a SiteMap
  5. Using ASP.Net Master Pages and Content Pages
  6. Validating Input using ASP.Net
  7. Tracing and Remote Debug in ASP.Net
  8. Creating Custom Error Pages with ASP.Net
  9. Managing Session State in ASP.Net
  10. Using Themes and Skins in ASP.Net
  11. Creating User Controls in ASP.Net
  12. Difference between ASP.Net User and Custom Server Controls
  13. Creating Fully Themable Websites with ASP.Net
  14. Ultimate Guide to the Web.Config File
  15. Adding StyleSheets to ASP.Net via C#
  16. Extending the Web Sitemap Xml Document

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

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me