Dynamic Content base class
Have made myself a base class which implements the dynamic content interface and have some functions to save and retrive the state based on what properties are in the PropertyDataCollection. Its pretty straight forward but thought I was going to share it anyway.
You use it by inherit from it, and set your propertyes like this:
public class MediaDynamicContent : DynamicContentBase
{
public override EPiServer.Core.PropertyDataCollection Properties
{
get
{
if (base.Properties == null)
{
PropertyDataCollection props = new PropertyDataCollection();
props.Add("Media", new EPiServer.SpecializedProperties.PropertyDocumentUrl());
props.Add("Width", new PropertyNumber(140));
props.Add("Height", new PropertyNumber());
SetProperties(props);
}
return base.Properties;
}
}
If you would like to use your own edit mode control, just refere to Properties["Media"].Value to get or set values.
The base class looks like this:
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using EPiServer.Core;
namespace Itera.DynamicContent
{
public class DynamicContentBase : EPiServer.DynamicContent.IDynamicContent
{
public string Add2Style = "";
public string PreviewText = "";
#region IDynamicContent Members
public virtual System.Web.UI.Control GetControl(EPiServer.PageBase hostPage)
{
throw new NotImplementedException();
}
PropertyDataCollection _properties;
public void SetProperties(PropertyDataCollection props)
{
_properties = props;
}
public virtual EPiServer.Core.PropertyDataCollection Properties
{
get
{
return _properties;
}
}
public virtual string Render(EPiServer.PageBase hostPage)
{
throw new NotImplementedException();
}
public virtual bool RendersWithControl
{
get { return true; }
}
#region State (64 encoded)
string _state = null;
public string State
{
get
{
if (Value == null)
return null;
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes((string)Value));
}
set
{
if (value != null)
{
_state = value;
byte[] toDecodeByte = Convert.FromBase64String(value);
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
int charCount = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);
char[] decodedChar = new char[charCount];
utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
string result = new String(decodedChar);
Value = result;
}
}
}
#endregion
#region Value
public string Value
{
get
{
if (Properties == null)
return null;
XmlDocument doc = new XmlDocument();
string xml = "<data>";
foreach (PropertyData prop in Properties)
xml += "<" + prop.Name + " />";
xml += "</data>";
doc.LoadXml(xml);
foreach (XmlNode node in doc.FirstChild.ChildNodes)
node.InnerText = Properties[node.Name].ToString();
return doc.FirstChild.InnerXml;
}
set
{
if (Properties == null)
return;
XmlDocument doc = new XmlDocument();
string xml = "<data>" + value + "</data>";
doc.LoadXml(xml);
foreach (PropertyData prop in Properties)
if (doc.FirstChild[prop.Name] != null)
prop.ParseToSelf(doc.FirstChild[prop.Name].InnerText);
}
}
#endregion
#endregion
}
}
Download
Base class is here http://paste2.org/p/109347
01 December 2008