Lee Crowe
May 20, 2011
  7137
(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
Optimizely Web Experimentation Metrics

Within the domain of Optimizely Web Experimentation Metrics, the emphasis is on objective key performance indicators (KPIs) selected to assess an...

Matthew Dunn | Sep 23, 2023 | Syndicated blog

Optimizely For you Intranet

Having been at Optimizely and in the CMS industry for nearly 16 years I have seen clients’ intranet requirements go from a simple site just to hous...

Robert Folan | Sep 22, 2023

Vulnerability in EPiServer.GoogleAnalytics v3 and v4

Introduction A potential security vulnerability was detected for Optimizely Google Analytics addon (including EPiServer.GoogleAnalytics and...

Bien Nguyen | Sep 20, 2023

Overriding Optimizely’s Content Recommendations Block to Implement Custom Recommendations

Introduction The Content Recommendations add-on for Optimizely CMS dynamically recommends content from your site tailored to the interests of each...

abritt | Sep 13, 2023 | Syndicated blog