Creating ASPX master pages

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

Kentico CMS allows you to use standard ASP.NET 2.0 master pages together with ASPX page templates. This is a very powerful concept which allows you to share the same site header and footer with a 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 the .master extension. 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 that use this master page should be loaded. So the master page typically contains the main logo and navigation elements and the content is displayed by ASPX pages loaded into the master page.

 

The following code sample defines a very simple master page.

 

Please note: If you installed the Kentico CMS project as a web application, you need to rename the CodeFile property on the first line to Codebehind for the code example to be functional.

 

<%@ 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="cms" %>

<%=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="form2" runat="server">

 

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

        <cms: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 the loading of content from the database into 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 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" />

 

In the code behind file, you need to add a reference to the CMS.UIControls namespace:

 
[C#]
 

using CMS.UIControls;

 

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

 
[C#]
 

public partial class CMSTemplates_CorporateSiteASPX_Root : TemplateMasterPage

 

And you also need do add 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 website.

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?using_the_master_pages.htm