This technical article was first published over 2 years ago and may discuss features and approaches that are no longer relevant for the current version of the platform.

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; } }
   }
}