Go Back

Hiding Control Properties from Sitefinity

By default, the Sitefinity CMS will automatically generate input fields for any public properties found in a control.  Look at the following code:

private string _test1;
private string _test2;
private string _test3;

public string Test1
{
    get { return _test1; }
    set { _test1 = value; }
}

public string Test2
{
    get { return _test2; }
    set { _test2 = value; }
}

public string Test3
{
    get { return _test3; }
    set { _test3 = value; }
}

In this example I've defined 3 public properties (Test1, Test2, Test3).  If this code were used in a custom User Control, Sitefinity would automatically generate 3 input fields for these properties:

Sitefinity Control Properties

This happens automatically for any public control properties.  Furthermore, Sitefinity will generate input fields for not only your public properties, but also public properties inherited from the System.Web.UI.UserControl base class.

Sitefinity Inherited Custom Control Properties

Depending on how many public properties your control has, this can become a bit overwhelming. 

Hiding Properties with Design-Time Attributes

Let's ignore Sitefinity for a moment and look at Visual Studio.  If I drop an instance of this User Control onto an ASPX page in Visual Studio, Visual Studio will automatically generate input fields for the public properties found in the control:

Visual Studio Custom User Control Properties

Notice this looks very similar to the properties' window Sitefinity is generating for the control.  Both Visual Studio and Sitefinity use reflection to find public properties at design-time.

Thankfully we can pass some hints to design-time tools by decorating public properties with design-time attributes.  We can hide public properties by using the Browsable attribute.  This will hide the property for both Visual Studio & Sitefinity.

If we want to hide all properties except the Test1 property we could use the following code:

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

public partial class UserControls_CustomControl : System.Web.UI.UserControl
{

    private string _test1;
    private string _test2;
    private string _test3;

    public string Test1
    {
        get { return _test1; }
        set { _test1 = value; }
    }

    [Browsable(false)]
    public string Test2
    {
        get { return _test2; }
        set { _test2 = value; }
    }

    [Browsable(false)]
    public string Test3
    {
        get { return _test3; }
        set { _test3 = value; }
    }

    [Browsable(false)]
    public override bool EnableTheming
    {
        get
        {
            return base.EnableTheming;
        }
        set
        {
            base.EnableTheming = value;
        }
    }

    [Browsable(false)]
    public override bool EnableViewState
    {
        get
        {
            return base.EnableViewState;
        }
        set
        {
            base.EnableViewState = value;
        }
    }

    [Browsable(false)]
    public override bool Visible
    {
        get
        {
            return base.Visible;
        }
        set
        {
            base.Visible = value;
        }
    }

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

The Browsable attribute sits in the System.ComponentModel namespace.   Your code will need to be using this namespace in order to decorate your properties with this attribute.  You can also override the public properties found in your base class and then decorate these overrides with the Browsable attribute.

Sitefinity Hidden Control Properties

Note: It is not possible to hide the ID property.  This property is required and rendered by default.

For more information, visit the Managing Properties with Attributes Sitefinity documentation page.

Comments  1

  • Amer Ghanem 28 Jan

    Hello,

    Thanks for the nice post. I was wondering if there is any reason why the properties won't be sorted they way I have them in the .cs file. In my case I have over 15 public properties and it will make a lot of sense if they are sorted in a specific way.

    In my code I have sorted in a certain sequence but the input fields generated are not in the same sequence.

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