Mark Everard

The only consistency is change itself

Removing the empty option from DynamicList properties

with 4 comments

Today seems to be one of those days where you pick up really simple bits of information that you’re surprised you never knew before (see Erik’s post about sending mail in a development environment for example)……

My little snippet comes from a question posted in the EPiServer forums that I responded to; about setting default values on EPiServer DynamicList properties. I’d always wondered how to remove the blank option that’s added by default to the edit mode DropDownList, though never enough to put some code together to actually get rid of it ;)

This morning though, I did just that, and it turned out to be very straightforward as the EPiServer development team had already provided an extensibility point for exactly that scenario.

All that’s needed is a new Property and PropertyControl class that overrides the existing PropertyAppSettings. This class defines a property called AutoGenerateEmptyValue. Override this property to return false to remove the empty value.

using System;
using EPiServer.Core;
using EPiServer.PlugIn;
using EPiServer.SpecializedProperties;
using EPiServer.Web.PropertyControls;

namespace MySite.CustomProperties
{
   [Serializable]
   [PageDefinitionTypePlugIn(DisplayName = "Dynamic list (one option) with no empty value", Description = "Works as the out-of-the-box Dynamic list property but displays no empty option")]
   public class PropertyAppSettingsNoEmptyValue : PropertyAppSettings
   {
      public override IPropertyControl CreatePropertyControl()
      {
         return new PropertyAppSettingsNoEmptyValueControl();
      }
   }

   public class PropertyAppSettingsNoEmptyValueControl : PropertyAppSettingsControl
   {
      protected override bool AutoGenerateEmptyValue { get { return false; } }
   }
}

Written by Mark

August 2nd, 2010 at 4:44 pm

Posted in ASP.NET,C#,EPiServer

4 Responses to 'Removing the empty option from DynamicList properties'

Subscribe to comments with RSS or TrackBack to 'Removing the empty option from DynamicList properties'.

  1. Works like a charm! Thank you :)

    Karoline Klever

    30 Sep 10 at 12:07 pm

  2. Brilliant Mark, thanks for sharing this.

    jason

    22 Mar 11 at 1:48 pm

  3. How do you add your new class such that it overrides the base class? I just wondering where it should be called from. Do you add something to your PageType file (the .aspx)?

    Jesper

    1 Dec 11 at 5:43 pm

  4. Hello Jesper, this code would be an entirely separate class in your codebase, which EPiServer will then include as a new property definition on startup.

    To use this on a page, when setting up your EPiServer PageType (either through code using PageTypeBuilder, or using the Admin interface) you would define a property which is of this type. The new property you define will display in the EPiServer edit mode as a drop-down list with no empty default value.

    Mark

    1 Dec 11 at 6:05 pm

Leave a Reply