Enhanced BooleanField control for SharePoint Publishing

For the first time today, I had to include a boolean field on a publishing page while in edit mode. (Doesn't that seem a bit weird? After all this time, there was never a need for content authors to update a boolean field.)

My field (site column) is nothing out of the ordinary. A Yes/No column with a name and a description. When the field is rendered on the list New/Edit/Display form, the title is on the left and the checkbox is on the right. The description is below the box, providing some context to the author. The display is accomplished via the InputFormSection control and the rendering templates in CONTROLTEMPLATES\DefaultTemplates.ascx.

In my publishing scenario, the value of the field is used to do some conditional rendering. It is not rendered in display mode, so on the page layout it is wrapped in an EditModePanel. And, as you would expect, I used a Microsoft.SharePoint.WebControls.BooleanField control. The result was pretty lame:

The BooleanField control also uses the RenderingTemplate from CONTROLTEMPLATES\DefaultTemplates.ascx. But it does not use the InputFormSection. The template for BooleanField is quite spartan:

The CheckBox does not have the Text attribute specified, so there is nothing next to the box as is the custom in ASP.NET pages.

Fixing the issue

My fix for the issue is a custom FieldControl to adjust the rendering. (This is not a custom FieldType. I don't need to change the storage of the field value, so a FieldType is not required.) In this custom control, I get a reference to the checkbox and set its Text attribute.

using System;  
using System.Web.UI.WebControls;  
namespace Schaeflein.Community.WebControls  
{
  public class FieldBooleanEnhancedControl: BooleanField
  {
    protected CheckBox cbx;
    protected override void CreateChildControls()
    {
      base.CreateChildControls();
      string fieldDescription = this.Field.Description;
      if (!String.IsNullOrEmpty(fieldDescription))
      {
        cbx = (CheckBox)this.TemplateContainer.FindControl("BooleanField");
        if (cbx != null && String.IsNullOrEmpty(cbx.Text))
        {
          cbx.Text = fieldDescription;
        }
      }
    }
  }
}

Since the checkbox control now has its Text attribute, the edit-mode experience is less lame, and authors are happy.