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.


Latest comments