Time zone property for EPiServer 6

PLACE FOR BLOG

Time zone property for EPiServer 6

Time zone property for EPiServer 6

marija

In order to define the property, two classes are needed:

  • PropertyTimeZone and
  • PropertyTimeZoneControl.

PropertyTimeZone inherits from PropertyDropDownList and implements CreatePropertyControl(). In this method, the other class - PropertyTimeZoneControl is instantiated. PropertyTimeZone needs to be decorated with PageDefinitionTypePlugIn, which tells EPiServer that it's a property type.

    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "Time zone", Description = "The drop down list with all time zones")]
    public class PropertyTimeZone : PropertyDropDownList
    {
        /// 
        /// Create property control
        /// 
        public override IPropertyControl CreatePropertyControl()
        {
            return new PropertyTimeZoneControl();
        }
    }

PropertyTimeZoneControl, on the other hand, inherits from PropertyDropDownListControl and implements SetupEditControls, where we basically draw the dropdown with our values.

    /// 
    /// This control overrides the SetupEditControls method to load dropdown with time zones
    /// 
    class PropertyTimeZoneControl : PropertyDropDownListControl
    {
        /// 
        /// Override SetupEditControls method to load dropdown with time zones
        /// 
        protected override void SetupEditControls()
        {
            var timeZones = TimeZoneInfo.GetSystemTimeZones();
            string selectedValue = this.ToString();
            foreach (var timeZoneInfo in timeZones)
            {
                var item = new ListItem(timeZoneInfo.DisplayName, timeZoneInfo.Id);
                item.Selected = selectedValue == timeZoneInfo.Id;

                base.EditControl.Items.Add(item);
            }
        }
    }

To use the above mentioned properties, simply define a property of type PropertyTimeZone:

        [PageTypeProperty(
            Type = typeof(PropertyTimeZone),
            Tab = typeof(CustomerSettings),
            UniqueValuePerLanguage = false,
            SortOrder = 50)]
        public virtual string TimeZone { get; set; }

Now go to edit mode and select your time zone :)

LEAVE A COMMENT