Kentico CMS 7.0 Tutorial ASPX

Using master pages

Using master pages

Previous topic Next topic Mail us feedback on this topic!  

Using master pages

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

You can use standard ASP.NET master pages together with ASPX page templates. This is a powerful concept that allows you to share content across all pages without having to add it separately to every page template. For example, you can create master pages containing header and footer sections with a logo, navigation menu, search box etc.

 

Master pages are defined in files with the .master extension. You can assign one master page to every ASPX page. Master pages must always contain one or more ContentPlaceHolder controls as shown here:

 

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

 

The ContentPlaceHolder control specifies where child pages display their content inside the master page.

 

It is recommended to store master pages in the CMSTemplates folder together with the page template files, so that the system can export them along with your website when you deploy it to another instance of Kentico CMS.

 

Creating master pages for ASPX templates

 

The following code sample shows the markup of a basic master page.

 

 

InfoBox_Exclamation

 

Important!

 

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. This attribute's value needs to match the name of the master page's code behind file.

 

Set the value of the Inherits attribute according to the location and name of the master page file.

 

 

<%@ 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">
          <ajaxToolkit:ToolkitScriptManager ID="manScript" runat="server" EnableViewState="false" ScriptMode="Release" />
          <cms:CMSPortalManager ID="CMSPortalManager1" runat="server" EnableViewState="false" />
      </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>

 

All ASPX page templates require the following manager controls, so it is a good practice to add them onto your website's master page:

 

ajaxToolkit:ToolkitScriptManager - allows pages to use AJAX components. This is required by all pages that contain the CMSPortalManager control.

CMSPortalManager - ensures the transferring of content between the database and editable regions. It also provides the management functionality needed for portal engine zones. Always place this control after the ToolkitScriptManager control.

 

 

InfoBox_Note

 

CMSPageManager control

 

You may use the CMSPageManager control instead of the CMSPortalManager. This control is an older equivalent available for the purposes of backwards compatibility. It does not support ASPX templates containing portal engine web part or widget zones.

 

The CMSMenu control is one of the options that you can use to generate a drop-down menu for website navigation.

 

Writing the master page code behind

 

You also need to modify the code behind file of the master page:

 

1. Add a reference to the CMS.UIControls namespace:

 
[C#]
 

using CMS.UIControls;

 

[VB.NET]

 

Imports CMS.UIControls

 

2. Change the class definition to match the following (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

 

Master pages must always inherit from the TemplateMasterPage class.

 

3. Add the following code into the master page's code behind class:

 

[C#]

 

protected override void CreateChildControls()
{
    base.CreateChildControls();
    PageManager = CMSPortalManager1;
}
 
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    this.ltlTags.Text = this.HeaderTags;
}

 

[VB.NET]

 

Protected Overrides Sub CreateChildControls()
    MyBase.CreateChildControls()
    PageManager = CMSPortalManager1
End Sub
 
Protected Overrides Sub OnPreRender(e As EventArgs)
    MyBase.OnPreRender(e)
    Me.ltlTags.Text = Me.HeaderTags
End Sub

 

Adjust the value of the PageManager property according to the ID of the CMSPortalManager (or CMSPageManager) control placed on the master page.

 

This code ensures that ASPX templates using the given master page support all required functionality.