Sitefinity CMS

Exposing Complex Child Controls as Properties Send comments on this topic.
Developing with Sitefinity > Controls > Adding New Controls to Sitefinity > Exposing Complex Child Controls as Properties

Glossary Item Box

You may have noticed that our Links List control uses standard ASP.NET BulletedList control for displaying the list of links. It may be beneficial if our end users could modify the properties of BulletedList since many of its properties are very usable. For example, the BulletedList property has Target property which defines where should the link open when user clicks on it. By default, a link will open in the same window, but if we change the Target property to "_BLANK", the link will open in a new window.

This topic explains how to expose the properties of complex child controls of a control. 

 

Exposing Properties of Child Controls as Properties of Sitefinity Control

So the first thing that we can do is to expose the Target property of BulletedList control as a property of our LinksList control. To do so we are going to create a new public property on the LinksList control, name it LinkTarget and map it to the Target property of the BulletedList.

C# Copy Code
/// <summary>
/// Gets or sets the target of the link (defines in which window will link open)
/// </summary>
[Category("List settings")]
public string LinkTarget
{
   get
   {
       
// we are directly referencing property of the linksBulletedList child control
       
// and returning the value of its target property
       
return linksBulletedList.Target;
   }
   set
   {
       
// we are directly referencing property of the linksBulletedList child control
       
// and returning the value of its target property
       
linksBulletedList.Target = value;
   }
}

 

As you can see in the get accessor we are returning the value of linksBulletedList Target property, while in the set accessor we are setting the value of linksBulletedList Target property to the new value. You may have also noticed that we have assigned this property to "List settings" category - the same one that our default property ControlTitle belongs to.

 

Exposing Complex Child Controls as Properties of Sitefinity Control

While we have managed quite easily to map the Target property of the BulletedList control to the LinkTarget property of our Sitefinity control, there are times when you would like to expose many or even all properties of the child control. Although you may do so by mapping all the properties, this would be a cumbersome task if the child control has large number of properties. Sitefinity luckily offers another possibility: you can expose the control itself as a property and Sitefinity will automatically handle this in the property grid. Even more – Sitefinity will also persist the properties of the child control for you. To expose the BulletedList control as a property of Links List control we will add another property to our Links List control:

C# Copy Code
/// <summary>
/// Exposes linksBulletedList child control as a property of the LinksList control, which
/// will allos us to modify the properties of the BulletedList control directly through
/// Sitefinity
/// </summary>
[Category("List settings")]
public BulletedList LinksBulletedList
{
   get
   {
      
return linksBulletedList;
   }
}

 

There are two important things to note here:

  • the type of the property has to be the same as the type of the child control that we are exposing
  • this property must be read-only (have only get accessor)

As you can see all that we are doing here is returning the instance of BulletedList control we have in our Links List control (ID of the BulletedList control is "linksBulletedList"). By doing so, our property grid will now look like this:

 Controls - Exposing Complex Child Controls - Parent control

Figure 1: Exposing child control as a property of control

 

When user clicks on the "Edit" button she will get new property grid for editing the properties of the child control. In our case it will look like this:

Controls - Exposing Complex Child Controls - Child control

Figure 2: Properties of BulletedList control (child control) exposed as properties of Links List control

 

Now, the end user has the ability to change the properties of BulletedList control such as BulletStyle, BulletImageUrl, FirstBulletNumber and so on without having to open our control in Visual Studio. At the same time we did not have to map all the properties of the child control in order to enable this feature.


You can download this control from here: LinksList4.