How to get EPiServer Property to display fall back value

Vote:
 

What is the best way to display a page property in a template or page, that will allow an editor to edit the value, but for a site visitor the fall back value will be displayed.

In a MasterTemplate, I want to display the page Heading (to show result I have put both a literal and then "//" and the EPiServer Property tag. The EPiServer property tag does not display the PageName (there is no value for Heading on this page.

The page property code is:

[Display(
			Name = "Heading",
			Description = "H1 header above content on page",
			GroupName = SystemTabNames.Content,
			Order = 310)]
		[CultureSpecific]
		public virtual string Heading
		{
			get
			{
				var pageHeading = this.GetPropertyValue(p => p.Heading);

				// Use explicitly set meta title, otherwise fall back to page name
				return !string.IsNullOrWhiteSpace(pageHeading) ? string.Concat("-", pageHeading, "-") : string.Concat("x", PageName, "x");
			}
			set { this.SetPropertyValue(p => p.Heading, value); }
		}

   

 

The code behind for the template is

Models.Pages.BaseContent pageContent = currentData as Models.Pages.BaseContent;
	if (pageContent != null)
	{
		ltlHeader.Text = HttpUtility.HtmlEncode(pageContent.Heading);
	}

    

The only way I can think at the moment is to add a test for being in edit mode (if (PageEditing.PageIsInEditMode)...) and hide the approrpriate controls. Is there a better way?

#66489
Mar 01, 2013 13:19
Vote:
 

You can use any type of control that outputs an element, meaning it must inherit from either WebControl or HtmlControl. For instance a Label works but not a Literal.

If you do that you can set its value to the Heading property like you do with the Literal above. Finally you can use the ApplyEditAttributes method to make it act as a property container for editing, like this:

labelHeader.ApplyEditAttributes(x => x.Heading)

If you do this the property will be rendered with fallback but if the editor clicks on it an empty textbox will appear (if they haven't entered a Heading) where they can enter the Heading.

#66497
Mar 01, 2013 14:04
Vote:
 

Thanks.

I found that this did not work in a MasterTemplate, but did inside a user control and aspx page.

As a note, the code behind page needs the using statements

using EPiServer.Web;
using EPiServer.DataAnnotations;

    

and the code to populate the control is (here with an extension to HTML encode the output)

hdrTitle.InnerText = CurrentPage.Heading.ToWebString();
hdrTitle.ApplyEditAttributes<Models.Pages.BasePageData>(p => p.Heading);

    

 

 

#66500
Mar 01, 2013 15:46
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.