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="Root.master.cs" Inherits="CMSTemplates_CorporateSiteASPX_Root" %>

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

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

<%=DocType%>

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

<head id="Head1" runat="server">

   <asp:literal runat="server" id="ltlTags" enableviewstate="false" />

</head>

<body class="<%=BodyClass%>" <%=BodyParameters%>>

 

   <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.

 

In case that you are planning to use AJAX components on your site, you need to add the ScriptManager control after the CMSPageManager control.

 

<asp:ScriptManager ID="manScript" runat="server" />

 

Switch to code behind. You need to add reference to the CMS.UIControls namespace:

 

using CMS.UIControls;

 

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

 

public partial class CMSTemplates_CorporateSiteASPX_Root : TemplateMasterPage

 

And you also need do put the following code to the master page code-behind class:

 

[C#]

 

protected override void CreateChildControls()

{

   base.CreateChildControls();

 

   this.PageManager = this.CMSPageManager1;

}

 

protected override void OnPreRender(EventArgs e)

{

   base.OnPreRender(e);

 

   this.ltlTags.Text = this.HeaderTags;

}

 

[VB.NET]

 

Protected Overloads Overrides Sub CreateChildControls()

   MyBase.CreateChildControls()

 

   Me.PageManager = Me.CMSPageManager1

End Sub

 

Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)

   MyBase.OnPreRender(e)

 

   Me.ltlTags.Text = Me.HeaderTags

End Sub

 

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