Go Back

Making Control Editing User-Friendly with Sitefinity Control Designers

Sitefinity ASP.NET CMS can be customized very easily using traditional ASP.NET technologies.  This makes it possible for .NET developers to create new functionality by adding tools.  These tools can then be used by editors (non-developers) to manage the web site. 

By default, Sitefinity will automatically generate a web-based interface for custom controls/tools.  For very simple controls, this interface is probably sufficient.  For complex controls, this interface can quickly become overwhelming. 

Sitefinity - This is NOT user friendly

The interface above fails to empower editors to manage the web site; it isn't user-friendly and will intimidate non-developers.  To remedy this, developers can use Control Designers to replace the default web-interface with a very user-friendly interface. 

Sitefinity User Friendly Control Designer

Creating a Custom Tool using an ASP.NET UserControl

Developers will primarily extend Sitefinity through ASP.NET Controls.  Below is a basic UserControl that displays Text with varying emphasis based on the Priority level.  (See code notes below.

~/App_Code/CustomControlBase.cs

[Telerik.Framework.Web.Design.ControlDesigner("CustomDesigner")]
public class CustomControlBase : System.Web.UI.UserControl
{
    public string Text { get; set; }

    public PriorityType Priority { get; set; }

    public enum PriorityType
    {
        Low,
        Medium,
        High
    }
}

~/Custom/UserControls/CustomControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs" Inherits="Custom_UserControls_CustomControl" %>
<p><asp:Label ID="Label1" runat="server" /></p>

~/Custom/UserControls/CustomControl.ascx.cs

using System;

public partial class Custom_UserControls_CustomControl : CustomControlBase
{
    private const string low = "font-size: 10px;";
    private const string medium = "font-size: 20px;";
    private const string high = "font-size: 30px;";

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Text;

        switch (Priority)
        {
            case PriorityType.Low:
                Label1.Style.Value = low;
                break;
            case PriorityType.Medium:
                Label1.Style.Value = medium;
                break;
            case PriorityType.High:
                Label1.Style.Value = high;
                break;
        }
    }
}

Add a Sitefinity tool mapping to this UserControl in the ~/web.config file:

<configuration>
  <telerik>
    <cms>
      <toolboxControls>
        <add name="Custom Control" section="My Controls" url="~/Custom/UserControls/CustomControl.ascx" />
      </toolboxControls>
    </cms>
  </telerik>
</configuration>

Code Notes

  • The CustomControlBase class facilitates the Control Designer's access to this control's properties.  (more information)
  • The UserControl inherits from the CustomControlBase class instead of the UserControl class.
  • The CustomControlBase class inherits from the UserControl class. 
  • The Control Designer is attached to this control using the ControlDesigner attribute. (more information)
  • The Control Designer referenced by the ControlDesigner attribute above does not yet exist; we'll create it in the step below.

Sitefinity Custom Control in the Page Editor

Creating a Custom Control Designer

In the step above a new UserControl and Control Base class was added to Sitefinity.  In addition, a Control Designer was attached to the UserControl.  In this step, we'll create the Control Designer and replace the default auto-generated interface with a user-friendly custom interface.

~/App_Code/CustomDesigner.cs

using System;
using System.Web.UI.WebControls;

public class CustomDesigner : Telerik.Framework.Web.Design.ControlDesigner
{
    /// <summary>
    /// Loads an external template that contains the controls & UI for the Control Designer.
    /// </summary>
    public override string LayoutTemplatePath
    {
        get { return "~/Custom/Admin/ControlDesigners/CustomDesigner.ascx"; }
    }

    /// <summary>
    /// Executed when Control Designer is initialized.
    /// </summary>
    protected override void InitializeControls(System.Web.UI.Control viewContainer)
    {
        // Populate drop-down and preset textbox and dropdownlist if we're not in a postback.
        if (!Page.IsPostBack)
        {
            TextBox1.Text = ((CustomControlBase) DesignedControl).Text;
            DropDownList1.DataSource = Enum.GetNames(typeof (CustomControlBase.PriorityType));
            DropDownList1.DataBind();

            ListItem item = DropDownList1.Items.FindByText(((CustomControlBase) DesignedControl).Priority.ToString());
            if (item != null)
            {
                DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(item);
            }
        }
    }

    /// <summary>
    /// Executed automatically when the I'm done button is clicked in the Control Designer.
    /// </summary>
    public override void OnSaving()
    {
        // Set the underlying control properties to the new values.
        ((CustomControlBase) DesignedControl).Text = TextBox1.Text;
        ((CustomControlBase) DesignedControl).Priority = 
            (CustomControlBase.PriorityType) Enum.Parse(typeof(CustomControlBase.PriorityType), DropDownList1.SelectedValue);
    }

    /// <summary>
    /// Gets a reference to the TextBox control contained in the LayoutTemplatePath template.
    /// </summary>
    protected virtual TextBox TextBox1
    {
        get { return base.Container.GetControl<TextBox>("TextBox1", true); }
    }

    /// <summary>
    /// Gets a reference to the DropDownList control contained in the LayoutTemplatePath template.
    /// </summary>
    protected virtual DropDownList DropDownList1
    {
        get { return base.Container.GetControl<DropDownList>("DropDownList1", true); }
    }
}

~/Custom/Admin/ControlTemplate/CustomDesigner.ascx

<%@ Control Language="C#" %>

<div class="ctrlProps">
    <div class="ctrlContent">
        <h3>Text to be displayed</h3>
        <p class="news-location">
            <asp:Label AssociatedControlID="TextBox1" Text="Enter text below:" EnableViewState="false" runat="server" />
            <asp:TextBox ID="TextBox1" Columns="40" runat="server" />
        </p>
        <p>
            <asp:Label AssociatedControlID="DropDownList1" Text="Priority:" EnableViewState="false" runat="server" />
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
        </p>
    </div>
</div>

Code Notes

  • LayoutTemplatePath can be used to automatically load an external template.
  • InitializeControls is executed automatically when the Control Designer is loaded.  This is a great place to preset web control values based on current control settings.
  • OnSaving is executed automatically when the I'm done button is clicked in the Control Designer.
  • There is no code-behind for the CustomDesigner.ascx template (UserControl).  You can create a code-behind file if you wish, but it's probably better to put your code-behind logic in the CustomDesigner class.

Conclusion

To get the most out of Sitefinity developers need to empower non-developers (marketing, administrators, editors, etc) to manage the web site.  Control Designers allow developers to do this by creating user-friendly admin. interfaces for their tools.

Comments  32

  • Lesley-Ann 12 Mar

    hi there tried to use your example to get to grips with this functionality (evaluating Sitefinity for a new site). InitializeControls code is only called the 1st time (not on postbacks), and so "OnSaving" being called, both the text and the dropdown have no data. similarly - moving between the basic and advanced tab - there is no data in text/dropdown on moving back. I fixed it by removing the "IsPostback" check in initialise controls, so that the state is initialised every time. Is this the right way to fix it? Or has something else gone wrong in my version, eg with viewstate? "enableviewstate" is not on on the container... Thanks, Lesley-An
  • Lesley-Ann 12 Mar

    hi there tried to use your example to get to grips with this functionality (evaluating Sitefinity for a new site). InitializeControls code is only called the 1st time (not on postbacks), and so "OnSaving" being called, both the text and the dropdown have no data. similarly - moving between the basic and advanced tab - there is no data in text/dropdown on moving back. I fixed it by removing the "IsPostback" check in initialise controls, so that the state is initialised every time. Is this the right way to fix it? Or has something else gone wrong in my version, eg with viewstate? "enableviewstate" is not on on the container... Thanks, Lesley-An
  • Romi 31 Mar

    Hi Gabe, Some way to add in the Dropdownlist the listofNews ( Title and ID) to use as selector for SingleNews. Regards
  • Sara 09 Jul

    Is this still valid in 3.6 and beyond?
  • Slawek 22 Oct

    Hi,

    Thanks for this article it helped a lot! I've created a product gallery control with 8 images and it works great.

    I want to make it a bit better now. In my ProductGalleryBase.cs I declare 16 strings 2x8 for image file name and image alt tag. Then in ProductGallery.ascs.cs I assign 5 properties for each image & hyperlink control. I can't find a way to do it in a loop which would be faster and cleaner way.

    If I declare 2 string arrays in ProductGalleryBase.cs will it work the same?

    Thanks,
    Slawek
  • Coffee table. 18 Mar

    Coffee table.
  • Lap desk. 22 Mar

    Sewing desk. Neon desk clocks. Roll top desk. Cflc reference desk.
  • Computer desk. 23 Mar

    Office desk. Secretary desk. Desk top wallpaper. Computer desk furniture office supply. Computer desk. Automated help desk. Writing desk. Help desk class.
  • Writing desk. 24 Mar

    Roll top computer desk. Corner computer desk. Van halen news desk. Desk calendars. Desk lamps. Lap desk. Computer desk. Art nouveau wooden desk.
  • Fiandra sofa. 26 Mar

    Fiandra sofa.
  • Distressing furniture. 27 Mar

    Distressing furniture.
  • Jarod rossignol. 01 Apr

    Jarod rossignol.
  • Digital issue october click edition. 03 Apr

    Digital camera reviews. Best digital cameras.
  • Paul karcher. 04 Apr

    Paul karcher.
  • Digital cameras. 06 Apr

    Kids digital camera. Digital picture frame. Compare digital cameras. Digital cameras. Kid digital camera. Digital camera review. Digital camera.
  • Digital camera reviews. 07 Apr

    Sony digital cameras. Digital picture frame. Digital cameras. Digital frame. Digital camera.
  • Laneventure. 08 Apr

    Laneventure everyday dining round pedestal table. Laneventure weathermaster platinum. Laneventure. Laneventure excursions old fabric selections. Weathermaster by laneventure. Laneventure weathermaster.
  • Sofa beds. 08 Apr

    Leather sofa. Montauk sofa. Sofa covers. Sofa. Conversation sofa spokane. Futon sofa.
  • 41 round upholstered tufted swivel ottoman. 09 Apr

    Pull-out ottoman. Islamic persian ottoman manuscripts. Ottoman slipcovers. Ottoman turks siege against constantinople. Ottoman empire. Storage ottoman. Round black storage ottoman.
  • Office casegoods. 09 Apr

    Hotel casegoods. Mitchell plastics and casegoods. Office casegoods. Casegoods.
  • Digital cameras. 09 Apr

    Digital cameras.
  • Compare digital cameras. 10 Apr

    Digital issue october click edition. Digital voice recorder. Canon elph digital camera. Digital cameras. Cheap digital cameras. Digital camera. Digital photography.
  • Digital cameras ratings. 11 Apr

    Digital issue october click edition. Digital dream girls. Digital scales. Digital cameras. Digital camera review. Canon powershot digital camera. Buy digital products. Digital. Digital photography.
  • Vocal ac4 skis. 12 Apr

    Vocal ac4 skis. Vocal skis.
  • 686 ec6. 13 Apr

    686 onyx. Rs-686.
  • Doris burton. 13 Apr

    Doris burton.
  • Rossignol quicksilver. 14 Apr

    Rossignol quicksilver.
  • S w 686 grips. 15 Apr

    S w 686 cs-1. 686 smarty maven jacket. Smith wesson model 686. Sw 357 686. S w 686 race gun. Elna 686 overlock sewing machine price. 686 vs 687.
  • Aka thelma burton. 15 Apr

    Thelma burton. Aka thelma burton.
  • Snowboard seconds. 16 Apr

    Snowboard seconds.
  • 686 jackets. 16 Apr

    686 pants. 686. 686 smarty pants. 686 snowboard. 773-686-6770. Beretta 686 onyx pro. 686 snowboarding. Lanmeter 686.
  • 686 jacket. 17 Apr

    686. 686 clothing. 484-686-4307. 541-686-7555.
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: