Kentico CMS 6.0 Developer's Guide

Creating ASPX master pages

Creating ASPX master pages

Previous topic Next topic Mail us feedback on this topic!  

Creating ASPX master pages

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

Kentico CMS allows you to use standard ASP.NET 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. across all pages without having to create these sections on each page template again and again.

 

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 as shown here:

 

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

 

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 attribute on the first line to Codebehind for the code example to be functional. Also, the attribute's value must be set to the name of the master page's code behind file and the Inherits attribute must be set according to the location and name of the master page.

 

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

 
<%=DocType%>

 
<html xmlns="http://www.w3.org/1999/xhtml" <%=XmlNamespace%>>
<head id="Head1" runat="server">
  <title id="Title1" runat="server">My site</title>
  <asp:literal runat="server" id="ltlTags" enableviewstate="false" />
</head>
<body class="<%=BodyClass%>" <%=BodyParameters%>>
  <form id="form1" runat="server">
      <asp:PlaceHolder runat="server" ID="plcManagers">

      <cms:CMSPortalManager ID="CMSPortalManager1" runat="server" EnableViewState="false" />

      <ajaxToolkit:ToolkitScriptManager ID="manScript" runat="server" EnableViewState="false"

       ScriptMode="Release" />

      </asp:PlaceHolder>
      <cms:CMSMenu ID="cmsmenu1" runat="server" Cursor="Pointer" HighlightAllItemsInPath="true"

            Layout="Horizontal"

            Padding="0"

            Spacing="1" />
      <asp:ContentPlaceHolder ID="plcMain" runat="server">
      </asp:ContentPlaceHolder>    
  </form>
</body>
</html>

 

The CMSPortalManager control ensures the loading and saving of content between the database and editable regions. It also provides the management necessary for web part or widget zones if any are defined on child ASPX pages (as described in Adding portal engine functionality to ASPX templates).

 

 

 

CMSPageManager control (obsolete)

 

Older versions of Kentico CMS used the CMSPageManager control instead of the CMSPortalManager. It is still available for the purposes of backward compatibility, but it does not support web/part widget zones and we recommend replacing it.

 

The CMSMenu control generates a drop-down menu used for navigation. The ContentPlaceHolder control defines the area where the content of sub-pages should be loaded.

 

If you are planning to use AJAX components on the pages of your site, you need to add the ToolkitScriptManager control in addition to the CMSPortalManager control.

 

<ajaxToolkit:ToolkitScriptManager ID="manScript" runat="server" EnableViewState="false" />

 

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

 
[C#]
 

using CMS.UIControls;

 

[VB.NET]

 

Imports CMS.UIControls

 

The master page must be inherited from the TemplateMasterPage class, so the class definition must look like this (the name of the class may be different):

 
[C#]
 

public partial class CMSTemplates_CorporateSite_Custom : TemplateMasterPage

 

[VB.NET]

 

Partial Class CMSTemplates_CorporateSite_Custom

  Inherits TemplateMasterPage

 

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

 

[C#]

 

protected override void OnPreRender(EventArgs e)

{

  base.OnPreRender(e);

 

  this.ltlTags.Text = this.HeaderTags;

}

 

[VB.NET]

 

Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)

  MyBase.OnPreRender(e)

 

  Me.ltlTags.Text = Me.HeaderTags

End Sub

 

It is recommended to store master pages in the CMSTemplates folder together with page templates, so that they are exported with your website.