Sitefinity Standard Edition

Implementing Properties in Controls Send comments on this topic.
Developing with Sitefinity > Controls > Adding New Controls to Sitefinity > Implementing Properties in Controls

Glossary Item Box

One of the very handy features of Sitefinity controls is automatic data persistence. Every public property that is browsable (by default all public properties are browsable) will be persisted by Sitefinity, which means that you don’t have to worry about the data layer and saving the values of the properties to the database.

 

Public properties are very important because they give the end users ability to modify the behavior, appearance and function of the control directly through Sitefinity user interface without opening the control in Visual Studio or having any knowledge of programming. To illustrate this concept we will start by creating the first version of Links List control which will have no properties at all. You can download this version of Links List control from here: LinksList1

 

Creating a Simple Control

Our control consists of two child controls:

  • Label - displays the title of the control
  • BulletedList - displays list of links

 

ASPX Copy Code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LinksList1.ascx.cs" Inherits="SampleControls_LinksList1" %>
<
h3>
<
asp:Label ID="controlTitleLabel" runat="server" Text="Favorite blogs"></asp:Label>
</
h3>
<
asp:BulletedList ID="linksBulletedList" runat="server" DisplayMode="HyperLink">
   
<asp:ListItem Value="http://www.techcrunch.com">TechCrunch</asp:ListItem>
   
<asp:ListItem Value="http://www.haacked.com">you&#39;ve been HAACKED</asp:ListItem>
   
<asp:ListItem Value="http://weblogs.asp.net/scottgu/">Scott Gu&#39;s blog</asp:ListItem>
   
<asp:ListItem Value="http://www.hanselman.com/blog/">Scott Hanselman&#39;s Computer
   Zen
</asp:ListItem>
   
<asp:ListItem Value="http://arstechnica.com/index.ars">Ars Technica</asp:ListItem>
</
asp:BulletedList>

 

In the code behind we only have Page_Load event handler which does not execute any code:

C# Copy Code
using System;
public partial class SampleControls_LinksList1 : System.Web.UI.UserControl
{
   
protected void Page_Load(object sender, EventArgs e)
   {
   }
}

 

Once we upload this control to the Sitefinity toolbox, end users will be able to drag it onto the page, but the title of the control will always be "Favorite blogs" and the list of links will always have the same links. The only way to modify the title of the control or the list of links is to open the control in Visual Studio and modify its code. While this may work well enough for developers, this control would be useless for end users.

 

Making Control Title Configurable

The first step towards making our Links List control configurable for end users without opening the control in Visual Studio is to let them set the control title directly through Sitefinity. In order to do so we are going to modify the code behind in such a way to create a public property ControlTitle and set the text property of controlTitleLabel control to the value of ControlTitle property.

C# Copy Code
using System;
public partial class SampleControls_LinksList1a : System.Web.UI.UserControl
{
   
/// <summary>
   
/// Gets or sets the title of the control
   
/// </summary>
   
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;
}

 

As you can see from the code behind, we have added only few lines of code. We have created public string property called ControlTitle, created the private field for it named controlTitle and in the Page_Load event we simply set the text property of the controlTitleLabel control to the value of ControlTitle property. By adding only these few lines we have achieved our first goal – end users can configure the title of the control directly through Sitefinity.

 

Every public property that is browsable is automatically displayed in the property grid of Sitefinity controls.

So, when the user drags a control onto the page, hover over it and click on the “Edit” link, this is what they are going to see:

 Controls - Properties - Automatically exposing public properties

Figure 1 : Sitefinity automatically exposes all public properties in the property grid

 

You can download this control from here: LinksList1a.


As you can see it takes only few lines of code to make configurable Sitefinity controls.