Quick way to create checkboxlist / dropdownlist properties

In EPiServer there is a easy way to create a checkbox list property by extending some existing classes.

In this example I will fetch the children for the start page and display them. In a future blog post I will show you how to do the same with a dropdown list control, and also show you have to extend this with Settings for properties to make the properties much more dynamic and useful.

Check box list / multiple choice property

1. Create a new class and make that class extend EPiServer.Core.PropertyMultiValue. This class should also have the attribute PageDefinitionPlugIn to be recognized as a property.

[PageDefinitionTypePlugIn(DisplayName="Custom CheckBox list")]
public class PropertyCustomCheckBoxList : PropertyMultipleValue
{
public override EPiServer.Core.IPropertyControl CreatePropertyControl()
{
return new PropertyCustomCheckBoxListControl();
}
}

2. Create the property control and extend EPiServer.Web.PropertyControls.PropertySelectMultipleControlBase

public class PropertyCustomCheckBoxListControl : PropertySelectMultipleControlBase
{
}

3. Now we need to populate the property with some data. In the control class, override the method SetupEditControls and do some magic

protected override void SetupEditControls()
{
// Get children to start page
foreach (PageData page in DataFactory.Instance.GetChildren(PageReference.StartPage))
{
// Create list item
ListItem li = new ListItem(page.PageName, page.PageLink.ID.ToString());
// check if list item is selected
li.Selected = ((PropertyMultipleValue)PropertyData).IsValueActive(li.Value);
// add item to checkbox list
this.EditControl.Items.Add(li);
}
}

4. That’s it. Everything else is handled by the classes you extend. By the way, the value of the property is saved as a comma separated string.

5 thoughts on “Quick way to create checkboxlist / dropdownlist properties

  1. Per Nergard

    Nice! Allan: Have you considered hosting the project over at epicode instead over codeplex? There are couple of custom properties there already which would be candidates in the propetypack.

  2. Pingback: EPiServer Developer Resources | Frederik Vig - ASP.NET developer

  3. Jens Henrik Skuldbøl

    This is great. However, is there a way to not hardcode the path to the page from which to get children? I would like to be able to reuse this for multiple checkboxes with each their content.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA image
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>