EPiServer Dynamic Content rendered with usercontrol

Post date: Jan 18, 2010 7:20:19 PM

using System;using System.Collections.Generic;using System.Web;using EPiServer.DynamicContent;using EPiServer.Core;using EPiServer.PlugIn;namespace EPiServer.Labs.DynamicContent

{ [GuiPlugIn(Area=PlugInArea.DynamicContent,

Url="~/Labs/DynamicContent/DateTimeContentSettings.ascx")] public class DateTimeContent : IDynamicContent

{ PropertyDataCollection _pdc; public DateTimeContent() { _pdc = new PropertyDataCollection(); _pdc.Add("Format", new PropertyString()); _pdc["Format"].Value = "yyyy-MM-dd HH:mm:ss"; } #region IDynamicContent Members public System.Web.UI.Control GetControl(PageBase hostPage) { DateTimeControl dtc = (DateTimeControl)hostPage.LoadControl("~/Labs/DynamicContent/DateTimeControl.ascx"); dtc.Format = State; return dtc ; } public EPiServer.Core.PropertyDataCollection Properties

{ get { return _pdc; } } public string Render(PageBase hostPage) { return DateTime.Now.ToString(_pdc["Format"].Value.ToString()); } public bool RendersWithControl

{ get { return true; } } public string State

{ get { return (string)_pdc["Format"].Value; } set { _pdc["Format"].Value = value; } } #endregion }}

Create a web usercontrol with a drop down that will be used when configuring the dynamic content

using System;using EPiServer.DynamicContent;namespace EPiServer.Labs.DynamicContent

{ public partial class DateTimeContentSettings : DynamicContentEditControl

{ protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) ddFormat.SelectedValue = this.Content.State; } public override void PrepareForSave() { this.Content.State = ddFormat.SelectedValue; } }}

Add another web user control with a asp:literal control on it, this will be used to render the HTML in view mode

using System;namespace EPiServer.Labs.DynamicContent

{ public partial class DateTimeControl : EPiServer.UserControlBase

{ public string Format { get; set; } protected void Page_Load(object sender, EventArgs e) { } protected override void OnPreRender(EventArgs e) { litDateTime.Text = DateTime.Now.ToString(Format); base.OnPreRender(e); } }}

Add the dynamic content class to web.config

<!-- Define the Dynamic content controls that you allow to run on your site.--> <dynamicContent> <controls> <add description="Displays a property from any page" name="PagePropertyPlugin" type="EPiServer.DynamicContent.PlugIn.DynamicPageProperty, EPiServer" /> <add description="Test" type="EPiServer.Labs.DynamicContent.DateTimeContent, EPiServer.Templates.Public" name="DateTimeContent" /> </controls> </dynamicContent>