Go Back

Organizing Sitefinity Control Properties with Design-Time Attributes

The Sitefinity CMS can be easily extended with regular ASP.NET User Controls.  Sitefinity will automatically generate input fields for any public properties found in these controls.  This empowers content editors to easily modify control properties using only their web browser.

Sitefinity User Controls Full Properties Window

In this example, only the Title property is defined in my user control.  The other properties were inherited from the System.Web.UI.UserControl base class.

Using design-time attributes we can hide these properties from Sitefinity.  Alternately, we can organize these properties into categories and designate our Title property as the default.

Using Property Categories and Setting Defaults

Design-time attributes are not a Sitefinity-specific feature.  These attributes are used by design-time developer tools (such as Visual Studio).  Sitefinity, wherever possible, utilizes existing .NET conventions rather than "re-invent the wheel".  This prevents developers from needing to learn a new system.

The design-time attributes reside in the System.ComponentModel namespace.  Use the Category attribute to set the category for your property and the DefaultProperty attribute to set the focus to a specific property.

Here is a code example:

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

[DefaultProperty("Title")]
public partial class UserControls_CustomControl : System.Web.UI.UserControl
{
    private string _title;

    [Category("Main")]
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

Here is the Sitefinity properties' window for this control:

Sitefinity Properties Organized Window

The non-default categories (Behavior & Misc) are collapsed by default.  You can hide these properties using the Browsable attribute.

Conclusion

Using a few quick design-time attributes you can make the control properties' window a lot less intimidating.  For more information, visit the Managing Properties with Attributes Sitefinity documentation page.

We're not done though.  We have more options still available to us.  In my next few posts we'll look at Type Editors and Control Designers.

Comments  1

  • Jay Buys 21 Feb

    This is a great tip and I've been using it for some time now.  I was wondering if you know of a way to order the properties you have in any given category.

    For example...
    I have "Feature 1", "Feature 2", and "Feature 3" as categories, with each category having 3 strings like "Feature1Link", "Feature1Image", "Feature1Text".

    Sitefinity seems to put those in whatever order it feels like.  They might show up in my control like this:
    - Feature 1
        - Feature1Link
        - Feature1Image
        - Feature1Text
    - Feature 2
        - Feature2Text
        - Feature2Link
        - Feature2Image
    etc...

    Sitefinity doesn't seem to order these properties alphabetically, or by the order in which they're declared in the control.  I can't seem to figure out how it orders these and if there is any way to control that.

    It's not a huge problem but for large controls with many properties, it can be confusing if the properties are always in a different order.  Any suggestions?
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: