Just Code‎ > ‎

EPiServer Custom property

posted Jan 18, 2010, 10:51 AM by Peter Henell
using System;
using System.Collections.Generic;
using System.Text;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.Web.PropertyControls;

namespace EPiServer.Labs
{
    /// <summary>
    /// Custom PropertyData implementation
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName="TrueFalseNull")]
    public class PropertyTrueFalseNull : EPiServer.Core.PropertyString
    {
        // TODO: Override members of EPiServer.Core.PropertyString to provide your own logic.

        public override object Value
        {
            get
            {
                return State;
            }
            set
            {
                if (value == null)
                {
                    State = null;
                }
                else
                {
                    State = Convert.ToBoolean(value);
                }
            }
        }
        
        public bool? State
        {
            get
            {
                if (IsNull)
                    return null;
                return bool.Parse(String);
            }
            set
            {
                ThrowIfReadOnly();
                SetPropertyValue(value, delegate()
                {
                    this.String = value.ToString();
                }
                );
            }
        }

        public override EPiServer.Core.IPropertyControl CreatePropertyControl()
        {
            return new PropertyTrueFalseNullControl();
        }

    }
}
Control:
using System;
using System.Collections.Generic;
using System.Text;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Web.PropertyControls;
using EPiServer.Web.WebControls;
using System.Web.UI.WebControls;

namespace EPiServer.Labs
{
    /// <summary>
    /// PropertyControl implementation used for rendering PropertyTrueFalseNull data.
    /// </summary>
    public class PropertyTrueFalseNullControl : EPiServer.Web.PropertyControls.PropertySelectControlBase
    {
        /*
        Override CreateXXXControls to control the appearance of the property data in different rendering conditions.

        public override void CreateDefaultControls()        - Used when rendering the view mode.
        public override void CreateEditControls()           - Used when rendering the property in edit mode.
        public override void CreateOnPageEditControls()     - used when rendering the property for "On Page Edit".

        */

        /// <summary>
        /// Gets the PropertyTrueFalseNull instance for this IPropertyControl.
        /// </summary>
        /// <value>The property that is to be displayed or edited.</value>
        public PropertyTrueFalseNull PropertyTrueFalseNull
        {
            get
            {
                return PropertyData as PropertyTrueFalseNull;
            }
        }


        protected override void SetupEditControls()
        {
            EditControl.Items.Add(new ListItem("Null", ""));
            EditControl.Items.Add(new ListItem("True", true.ToString()));
            EditControl.Items.Add(new ListItem("False", false.ToString()));
            if (!PropertyTrueFalseNull.IsNull)
                EditControl.SelectedValue = PropertyTrueFalseNull.State.ToString();
        }
    }
}
Comments