Dynamic Content base class

by: Anders Hattestad

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


Comments

  1. Well done Anders! I have been planning to post something like this on my blog for ages but just haven't got around to it yet.
  2. Hi, I want to create one column and two column web part using episerver, asp.net, C#. how dynamically can be done, user decide at runtime that how may columns or rows he want to create and user decide that weather he want to create heading and subheading. Let me know, if there is anyway. Thanks
  3. Thanks! your code for handling state was just what i needed so that my dynamic content with linkitemcollection could handle "æøå" as link text!
  4. Cool that you could use it
Post a comment    
User verification Image for user verification  
EPiTrace logger