Lee Crowe
May 20, 2011
  7369
(1 votes)

Multiple Selection Custom Property Base Control

On many occasions I have built custom properties which allow a user to select and sort options from an available list.

I am sure there are many alternative solutions to the one I am presenting here but I decided to build my own base control that would contain the common functionality .

The base class is named ‘PropertyMultipleSelectionBaseControl’ and can be downloaded from here.

An example of the rendered control is shown below:

 

The base class has the following properties and methods:

 

PropertyMultipleSelectionBaseControl Properties

  • SelectedValues – A List of string that represent the selected options

PropertyMultipleSelectionBaseControl Virtual Properties

The following properties can be overridden in the derived implementation:

  • AvailableItemsText – Get the available items text
  • SelectedItemsText  - Gets the selected items text
  • AddButtonText – Gets the add button text
  • RemoveButtonText – Gets the remove button text
  • MoveUpText – The move up button tooltip text
  • MoveDownText – The move down button tooltip text
  • ListBoxWidth –The width in pixels of the list box
  • ListBoxRows – The number of rows in the list box
  • MinimumNumberOfSelectedValues – The minimum number of selected values
  • MaximumNumberOfSelectedValues – The maximum number of selected values
  • MinimumNumberOfSelectedValuesErrorMessageFormatString – The minimum number of selected values error message format string
  • MaximumNumberOfSelectedValuesErrorMessageFormatString – The maximum number of selected values error message format string

PropertyMultipleSelectionBaseControl Virtual Methods

The following methods can be overridden in the derived implementation:

  • GetBindableData – The method returns a Dictionary<string, string> object that the list boxes will be bound to.

Code Usage Examples

The following code demonstrates how the control can be used.

Simple Usage Example

This code example demonstrates how you would bind the list boxes to a collection of pages.
   1:  public class PropertySimpleMultipleSelectionPropertyControl : PropertyMultipleSelectionBaseControl
   2:  {
   3:      protected override string AvailableItemsText
   4:      {
   5:          get { return "Available Pages"; }
   6:      }
   7:   
   8:      protected override string SelectedItemsText
   9:      {
  10:          get { return "Selected Pages"; }
  11:      }
  12:   
  13:      protected override Dictionary<string, string> GetBindableData()
  14:      {
  15:          PageDataCollection pages = DataFactory.Instance.GetChildren(PageReference.StartPage);
  16:          return pages.ToDictionary(page => page.PageLink.ID.ToString(), page => page.PageName);
  17:      }
  18:  }

 

Advanced Usage Example

This code example demonstrates how you would create a control which comprises of the multiple selection functionality and other inputs.  In this example the additional input is a Text Box which will capture the number of pages entered.

   1:  public class PropertyContentProviderSelectionControl : PropertyMultipleSelectionBaseControl
   2:  {
   3:      private TextBox _pageCountTextBox;
   4:   
   5:      protected override string AvailableItemsText
   6:      {
   7:          get { return "Available Content Providers"; }
   8:      }
   9:   
  10:      protected override string SelectedItemsText
  11:      {
  12:          get { return "Selected Content Providers"; }
  13:      }
  14:   
  15:      protected override Dictionary<string, string> GetBindableData()
  16:      {
  17:          var contentProviders = new PersonalizationEngine().GetVisitorGroupContentProviders();
  18:          return contentProviders.ToDictionary(contentProvider => contentProvider.UniqueId.ToString(),
  19:                                                  contentProvider => string.Format("{0}, {1}, {2}", contentProvider.GetVisitorGroupName(),
  20:                                                                                  contentProvider.ContentProviderTypeDisplayName,
  21:                                                                                  Page.Server.HtmlEncode(contentProvider.ContentProviderCriteria)).TrimEnd(' ', ','));
  22:      }
  23:   
  24:      public override void ApplyEditChanges()
  25:      {
  26:          base.ApplyEditChanges();
  27:   
  28:          int pageCount;
  29:   
  30:          if (!int.TryParse(_pageCountTextBox.Text, out pageCount) || pageCount < 0)
  31:          {
  32:              AddErrorValidator("You must enter a valid page count");
  33:              return;
  34:          }
  35:   
  36:          SelectedContentProviders selectedContentProviders = new SelectedContentProviders
  37:          {
  38:              PageCount = pageCount,
  39:              ContentProviderIds = SelectedValues.Select(current => new Guid(current)).ToList()
  40:          };
  41:   
  42:          SerializationHelper.SerializeObject(selectedContentProviders);
  43:          SetValue(selectedContentProviders);
  44:      }
  45:   
  46:      public override void CreateEditControls()
  47:      {
  48:          SelectedContentProviders selectedContentProviders = PropertyData.Value as SelectedContentProviders ??
  49:                                                              new SelectedContentProviders();
  50:   
  51:          if (selectedContentProviders.ContentProviderIds == null)
  52:              selectedContentProviders.ContentProviderIds = new List<Guid>();
  53:   
  54:          SelectedValues = selectedContentProviders.ContentProviderIds.Select(current => current.ToString()).ToList();
  55:   
  56:          // add page count
  57:          Label label = new Label { Text = "Page Count:&nbsp;" };
  58:          Controls.Add(label);
  59:   
  60:          _pageCountTextBox = new TextBox { ID = "PageCount", Text = selectedContentProviders.PageCount.ToString() };
  61:          Controls.Add(_pageCountTextBox);
  62:   
  63:          // add base multiple selection editing controls
  64:          base.CreateEditControls();
  65:      }
  66:  }
May 20, 2011

Comments

Please login to comment.
Latest blogs
Add your own tools to the Optimizely CMS 12 admin menu

The menus in Optimizely CMS can be extended using a MenuProvider, and using the path parameter you decide what menu you want to add additional menu...

Tomas Hensrud Gulla | Oct 3, 2024 | Syndicated blog

Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024