Removing the empty option from DynamicList properties

R

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

About the author

Mark Everard

I've worked across the digital industry for the past ten years, helping clients and colleagues across a diverse range of sectors meet numerous digital challenges, specifically focusing on web technologies, digital marketing and content management.

I've worked on large multi-supplier projects and led and managed both in-house and geographically-disperse development teams. And I've always approached my work with a smile on my face.

Mark Everard

I've worked across the digital industry for the past ten years, helping clients and colleagues across a diverse range of sectors meet numerous digital challenges, specifically focusing on web technologies, digital marketing and content management.

I've worked on large multi-supplier projects and led and managed both in-house and geographically-disperse development teams. And I've always approached my work with a smile on my face.

Get in touch