ASP.NET v2.0 V: Master pages and navigation
This article is the fifth in a series looking at ASP.NET 2.0 though it follows on from my earlier overview articles on the subject. Here's what we've covered thus far:
I: we looked at the enhancements to existing controls. II: we proceeded to provide an overview of the new controls that have been introduced III: we looked at the changes to the compilation and deployment architecture in v2.0 as well as briefly looking at the tool support in VS .NET 2005. IV: we looked at data source controls and data binding via articles A and B.
In this article we look at master pages and navigation which comes down to how ASP.NET 2.0 aids in the design and consistency of sites. Previously bespoke coding would have been required to achieve these aims but now functionality is built in to assist in ASP.NET v2.0.
Master pages provide a template for all content pages of your web site thus facilitating a consistent look and feel. A master page defines a template which can include UI, code and any default content but it also defines content areas that will change on a per page basis using the ContentPlaceHolder control. Pages that user a master page to define the layout can place content only in the areas defined by the ContentPlaceHolder, thus enabling a consistent site design.
Master pages are ASP.NET pages with a different file extension (.master). No new knowledge is required apart from the use of the ContentPlaceHolder control. When you view the code for the page the only difference you will see is that the Master directive replaces the Page directive.
To make a page use the master page you include the masterpagefile attribute in the page directive. You then add Content controls to the page in the permitted area which are assigned a ContentPlaceHolder id. When the page is constructed ASP.NET first adds all the content from the master page. Then it loops through the ContentPlaceHolder controls and, for each, looks in the content page for a Content control where the ContentPlaceHolderID matches the id of the ContentPlaceHolder. This ensures the correct content is placed in the correct holder.
The master page can also supply default content which can be overridden by content pages. This is achieved simply by placing the content in the ContentPlaceHolder element.
Here's a 'real world' example from a client project (CWMasterPage.master):
<%@ Master Language="VB" CodeFile="CWMasterPage.master.vb" Inherits="CWMasterPage" %>
<%@ Register TagPrefix="CW" TagName="Header" Src="UserControls/CWHeaderControl.ascx" %>
<%@ Register TagPrefix="CW" TagName="Menu" Src="UserControls/CWMenuControl.ascx" %>
<!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 id="Head2" runat="server">
<!-- PAGE TITLE -->
<title id="PageTitle" runat="server">Design Wales Client Log</title>
<!-- STYLE SHEET -->
<link href="styles/CWStyles.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form id="frmCW" runat="server" enctype="multipart/form-data">
<asp:panel ID="pnlCW" Visible="true" runat="server">
<!-- HEADER CONTROL -->
<CW:Header ID="CWHeader" runat="server" />
<!-- MENU CONTROL -->
<CW:Menu ID="CWMenu" runat="server" />
<!-- MAIN PAGE CONTENT -->
<asp:contentplaceholder id="mainContentPlaceHolder" runat="server">
</asp:contentplaceholder>
</asp:panel>
<!-- UNEXPCTED URL WARNING -->
<asp:panel ID="pnlUnexpectedURL" Visible="false" runat="server">
<div class="CWWarningText" align="center">
<br /><br />
This website has been installed to an unexpected location. Please <a href='mailto:DW@cymruweb.net'>contact CW.net</a> to resolve.
</div>
</form>
</body>
</html>
And here is the corresponding default.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" MasterPageFile="~/CWMasterPage.master" %>
<asp:content id="CWContent" contentplaceholderid="mainContentPlaceHolder" runat="server">
<!-- CENTER COLUMN -->
<div id="CWCenterContent">
<br><br>
<asp:Image id="imgHome" runat="server" ImageUrl="~/Images/hdn_home.gif" /><br/><br/>
<div class="txtonwhite">
Welcome to Version <strong>3.0</strong> of the Client Log Database Application.
</asp:content>
Further points regarding master pages in summary form are:
Note that any locally defined master file in the page directive will override this setting.
<%@ Page MasterFilePage="default.master" Mozilla: MasterFilePage="mozilla.master" %>
Of course this technique comes in handier when the multiple devices you are supporting are very different and hence very different content and/ or amounts of content can be displayed. Device-specific support is also available on the Content control.
With ASP.NET 2.0 new controls and configuration files provide a set way to define site structure and techniques for displaying navigation information as well as extracting the current path within the structure. The architecture for navigation has been broken down into parts:
1. a configurable site map provider supplying the site map information as well as keeping track of the current position 2. a set of controls that can take advantage of the supplied data
Let's take a quick look at each.
A configurable site map provider supplies the site map information to other controls. Site maps are pluggable within the <system.web> section of web.config. The default provider for the SiteMap class is the XmlSiteMapProvider class, allowing site navigation structure to be stored in an XML file, Web.Sitemap. You also have the option of creating your own providers.
So, with the default provider in place, setting up site map data is involves creating a Web.Sitemap XML files that conforms to the required schema. This best demonstrated by example:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home" >
<siteMapNode title="Services" >
<siteMapNode title="Training" url="~/Training.aspx"/>
</siteMapNode>
</siteMap>
Then it is a matter of setting up a page with controls to use this data.
The two primary navigation controls are the Treeview and Menu, both of which can be bound to a SiteMapDataSource control. You also have access to the SiteMapPath control which displays a navigation path, also known as a breadcrumb, that shows the current page location and displays links as a path back to the home page.
Here are some code snippets by way of example:
<asp:SiteMapPath ID="SiteMapPath1" Runat="server">
</asp:SiteMapPath>
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
<h2>Using TreeView</h2>
<asp:TreeView ID="TreeView1" Runat="Server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
<h2>Using Menu</h2>
<asp:Menu ID="Menu2" Runat="server" DataSourceID="SiteMapDataSource1">
</asp:Menu>
<h2>Using a Horizontal Menu</h2>
<asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1"
Orientation="Horizontal"
StaticDisplayLevels="2" >
Simply placing the SiteMapDataSource on the page will by default utilise Web.Sitemap. The navigation controls can be styled with CSS and their inbuilt properties to achieve the required design effects and, as you’d expect from server controls, can be customised programmatically at runtime.
In this article we’ve introduced the new features in ASP.NET 2.0 aimed at improving the consistency of pages and the navigation between those pages of your web site. In the next article we’ll take a closer look at some of the new security controls in ASP.NET 2.0.
ASP.NET v2.0 – the Beta Version Homer et al. Addison-Wesley
.NET 2.0 SDK documentation