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.
Nice! Take a look at this project here http://propertypack.codeplex.com/ and see – I think we might be working on the same thing đ
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.
Pingback: EPiServer Developer Resources | Frederik Vig - ASP.NET developer
Thanks. Was looking for this exactly.
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.