Sitefinity CMS

Managing Properties with Attributes Send comments on this topic.
Developing with Sitefinity > Controls > Adding New Controls to Sitefinity > Managing Properties with Attributes

Glossary Item Box

In the previous topic [MAKE THIS A LINK] we have added the first property to the Links List control. In this topic we are going to examine how to use attributes to group the properties in a more meaningful way then they are by default. We will also see how to set the controls default.

 

Grouping Properties

In the previous sample (download from here: LinksList1) we have added ControlTitle property and the property was added to the "MISC" category (section) â€“ by default, all properties are added there. We are planning to add more properties to our control and we’d like our end users to have all the properties at one place – that is why in this example we are going to set the Category attribute of the ControlTitle property and set the category to be "List settings". To do so we are going to add the attribute in the following manner:

C# Copy Code
/// <summary>
/// Gets or sets the title of the control
/// </summary>
[Category("List settings")]
public string ControlTitle
{
  get
  {
      
// if controlTitle is empty, return default title "List"
      
if (String.IsNullOrEmpty(controlTitle))
         
return "List";
      
return controlTitle;
  }
  set
  {
      controlTitle = value;
  }
}

 

Making a Property the Default Property

Now that we have categorized this property under "List settings", we also want to make it the default property – the most important property. By doing so we will ensure that ControlTitle is the first property end user will see when editing our control. In addition to that, the default property is more emphasized by Sitefinity. All we have to do is to set the DefaultProperty attribute on the class declaration. The final version of this sample code behind looks like this:

C# Copy Code
using System;
using System.ComponentModel;

[DefaultProperty("ControlTitle")]
public partial class SampleControls_LinksList2 : System.Web.UI.UserControl
{
   
/// <summary>
   
/// Gets or sets the title of the control
   
/// </summary>
   
[Category("List settings")]
   
public string ControlTitle
   {
       get
       {
           
// if controlTitle is empty, return default title "List"
           
if (String.IsNullOrEmpty(controlTitle))
               
return "List";
           
return controlTitle;
       }
       set
       {
           controlTitle = value;
       }
   }
   
   
protected void Page_Load(object sender, EventArgs e)
   {
       
// set the text of title label to the value of ControlTitle property
       
controlTitleLabel.Text = ControlTitle;
   }
   
private string controlTitle;
}

 

And now, when the end user opens our control in edit mode, the screen that she will see will look like this:

Controls - Implementing Properties with Attributes

Figure 1: ControlTitle as the Default Property


 

You can download this control from here: LinksList2.