• Published
  • 1 min

Custom boolean property

As a developer working with dynamic properties, I am sure you have came across the fact that it is not possible to reset or empty an EPiServer boolean dynamic property. This is something I’ve needed several times, so here is a simple sample code to create a custom boolean property.

BooleanSelector.cs:
No changes made from the default EPiServer template

BooleanSelectorControl.cs:
Override CreateEditControls like this:

public override void CreateEditControls()
{
    base.CreateEditControls();

    DropDownList myList = this.EditControl;
    myList.Items.Add(new ListItem("False", "0"));
    myList.Items.Add(new ListItem("True", "1"));

    //set selected property values
    string val = this.ToString();
    if (val.Length > 0)
    {
      myList.SelectedValue = val;
    }
}