Go Back

Using Enumerators to Auto-generate a Dropdown

Sitefinity ASP.NET CMS can be extended very easily using traditional ASP.NET UserControls.  Once a UserControl is added to Sitefinity, Sitefinity will automatically generate a web admin interface.  This interface allows editors to edit any public properties found in the User Control.

Below is a very simple ASP.NET UserControl with two public properties (Text, Color).  These two properties can be used to display text in a chosen color.

~/Custom/UserControls/ColoredText.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ColoredText.ascx.cs" Inherits="Custom_UserControls_ColoredText" %>

<div id="Div1" runat="server"><asp:Literal ID="Literal1" runat="server" /></div>

~/Custom/UserControls/ColoredText.ascx.cs

using System;

public partial class Custom_UserControls_ColoredText : System.Web.UI.UserControl
{
    public string Text { get; set; }
    public string Color { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Div1.Style.Add("color", Color);
        Literal1.Text = Text;
    }
}

Sitefinity will automatically generate the following web admin interface for this control:

Sitefinity UserControl Editor

This UserControl causes the following HTML to be generated:

<div style="color:Red;">Hello world</div>

Using Dropdowns to Avoid Mistakes

This UserControl works fine until someone types an invalid HTML color name.  When this happens, this UserControl will generate invalid HTML:

<div style="color:JohnDeereGreen;">Hello world</div>

This is easily solved by using enum to declare an enumeration.  The Color property's type can then be set to this enumeration instead of a string.  Here is the new code:

~/Custom/UserControls/ColoredText.ascx.cs

using System;

public partial class Custom_UserControls_ColoredText : System.Web.UI.UserControl
{
    public string Text { get; set; }
    public ColorType Color { get; set; }
    public enum ColorType { Black, Blue, Red, Green, Purple, Gray, Yellow }

    protected void Page_Load(object sender, EventArgs e)
    {
        Div1.Style.Add("color", Color.ToString());
        Literal1.Text = Text;
    }
}

Sitefinity will automatically generate a dropdown from this enumeration:

Sitefinity Dropdown from Enum

Pretty neat, eh?  As much as possible your UserControls should make it impossible for editors to do the wrong thing.  Enumerators can be used to prevent editors from making mistakes.

This is just the beginning though.  To make the control editing experience even better, check out WebEditors and Control Designers.

P.S.  I think "John Deere Green" (#3e7126) should be an official HTML color.   :)

Comments  2

  • Kevin McClain 05 May

    Gabe, this is amazingly helpful! :) I am just getting into Sitefinity and I thought there was a way to do this without having to override a designer or create a new one. I am on a bit of a tight schedule for this first project so I cannot get into the designers right now, but I will certainly be coming back to read your other posts. :) Thanks again.
  • David 23 Sep

    Hi Gabe, your blog is excellent source of info.  This info help me learn how to created a module on the back end to hold some content. 

    I am looking for a way to populate the enumeration from this database table into a dropdown box for the control. 

    Is this possible? 

    Also can you do a video pod cast on that focuses just on the design control designer?  I am thinking this is the route I might have to take. 

    David
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: