Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Table of contents

Introduction

When working with content data in EPiServer CMS, the default way to render content in a template is to use the property web control, <EPiServer:Property> (see EPiServer.Web.WebControls.Property). That control will be responsible for adding the required HTML in order for it to be editable in the content editing view. However, if you render your content some other way you need to make sure that the output contains the HTML attributes that the content editing view requires.

So a common scenario would be to add a wrapping HTML element around the rendered content. Set runat="server" on the element and call the property from the code behind.

<EPiServer:Property PropertyName="MyProperty" runat="server" />

Applying edit attributes to a custom control

The best way to add the attributes required for editing is to use the method EPiServer.Web.ApplyEditAttributes. It is an extension method to the System.Web.UI.Control class. Add a wrapping HTML element around the rendered content. Set runat="server" on the element and call ApplyEditAttributes on it from the code behind.

Applying full refresh properties

Some properties can affect the rendering of several parts of the page. For instance, you may have a boolean value on your page type that enables/disables several panels. To get a correct preview of such a property you would need to do a full refresh of the page.

To make a set of properties force a full reload on change, use the web control EPiServer.Web.WebControls.FullRefreshPropertiesMetaData. It will render an HTML element for all properties registered in the collection EPiServer.PageBase.FullRefreshProperties.

Example

The following example shows how to use these methods in a template:

C#
<EPiServer:FullRefreshPropertiesMetaData runat="server" />

<h1 runat="server" id="HeadingOne">
    <%-- Render the Heading property, unless it it empty, then use PageName as fallback --%>
    <%: CurrentPage.Heading ?? CurrentPage.PageName %>
</h1>

<EPiServer:Property Runat="server" PropertyName="MainBody" />
C#
using System;
using EPiServer;
using EPiServer.Core;
using EPiServer.Web;
using EPiServer.DataAnnotations;

namespace CodeSamples
{
    public partial class CustomEditHints : TemplatePage<MyPageType>
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Connect the content div to the Heading property of MyPageType
            HeadingOne.ApplyEditAttributes<MyPageType>(p => p.Heading);

            // Register the ShowBanner property for full refresh preview
            EditHints.AddFullRefreshFor(p => p.ShowBanner);
        }
    }

    [ContentType]
    public class MyPageType : PageData
    {
        public virtual string Heading { get; set; }
        public virtual XhtmlString MainBody { get; set; }
        public virtual bool ShowBanner { get; set; }
    }
}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Mar 31, 2014

Recommended reading