Creating ASPX master pages

Kentico CMS allows you to use standard ASP.NET 2.0 master pages together with ASPX page templates. This is a very powerful concept, that allows you to share the same site header and footer with logo, main menu, search box, etc. over all pages without having to create these sections on each page template again and again.

 

The master pages are defined in files with extension .master. You can assign a single master page to each ASPX page. The master page must always contain the ContentPlaceHolder control like this:

 

<asp:ContentPlaceHolder ID="plcMain" runat="server">

 

The ContentPlaceHolder control specifies where the content of page templates should be loaded. So the master page typically contains the main logo and navigation and the content is displayed by ASPX pages loaded into the master page.

 

The following code sample defines a very simple master page:

 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMenu.master.cs" Inherits="CorporateSiteASPX_MainMenu" %>

<%@ Register Assembly="CMS.Controls" Namespace="CMS.Controls" TagPrefix="cc1" %>

<%@ Register Assembly="CMS.PortalControls" Namespace="CMS.PortalControls" TagPrefix="cc2" %>

<%@ Register Assembly="CMS.CMSHelper" Namespace="CMS.CMSHelper" TagPrefix="cc1" %>

<%=mDocType%>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

   <title runat="server" id="titleTag" />

   <meta name="description" runat="server" id="metaDescription" content="" />

   <meta id="metaNoCache" http-equiv="pragma" content="no-cache" runat="server" />

   <meta name="keywords" runat="server" id="metaKeyWords" content="" />

   <link type="text/css" rel="stylesheet" runat="server" id="metaCssFile" />

   <asp:literal runat="server" id="ltlExtendedTags" enableviewstate="False" />

</head>

 

<body class="<%=CMSContext.CurrentBodyClass%>" <%=mBodyParameters%>>

 

<form id="form1" runat="server">

    <cc1:CMSPageManager ID="CMSPageManager1" runat="server" />

    <cc1:CMSMenu ID="cmsmenu1" runat="server" CSSPrefix=";Sub" Cursor="Pointer"

         HighlightAllItemsInPath="true"

         Layout="Horizontal"

         Padding="0"

         Spacing="1" />

    <asp:ContentPlaceHolder ID="plcMain" runat="server">

    </asp:ContentPlaceHolder>

</form>

</body>

 

</html>

 

 

The CMSPageManager control ensures loading of the content from the database into the editable regions. The CMSMenu control displays a drop-down menu. The ContentPlaceHolder control defines where the content of sub-pages should be loaded.

 

The master page must be inherited from the TemplateMasterPage, so the class definition must look like this:

 

 

 

public partial class CorporateSiteASPX_MainMenu : TemplateMasterPage

 

 

And you also need do put the following code to the Page_Load method of the master page:

 

[C#]

 

       this.metaCssFile.Href = this.mCssFile;

       this.metaDescription.Content = CMSContext.CurrentDescription;

       this.metaKeyWords.Content = CMSContext.CurrentKeyWords;

       this.titleTag.Text = CMSContext.CurrentTitle;

       this.ltlExtendedTags.Text = mExtendedTags;

 

[VB.NET]

 

       metaCssFile.Href = mCssFile

       metaDescription.Content = CMSContext.CurrentDescription

       metaKeyWords.Content = CMSContext.CurrentKeyWords

       ltlTitle.Text = CMSContext.CurrentTitle

       ltlExtendedTags.Text = mExtendedTags

 

You should store master pages in the CMSTemplates folder together with page templates, so that they are exported with your web site.