Tags

,

The RadTagCloud is the Tag Cloud control that is packed with the Sitefinity SDK (Software Development Kit). It could be used for displaying links in a TagCloud-like fashion. These items could be managed by the user in the Sitefinity backend, in the same way as with other widgets: by their public properties.

The RadTagCloud could be put in a markup file, like a usercontrol.

<telerik:RadTagCloud ID="RadTagCloud1" runat="server" />


Next, we want a number of properties that the user could edit in the backend. Each tagcloud item has three properties that should be defined: Text, NavigateUrl, and Weight. The Text property manages the linktext, NavigateUrl manages where the link points to, and Weight determines the size of the link in the tagcloud. For each tagcloud item, we need at least these three properties. So if the user wants to be able to add 15 items to the tagcloud, we have to write 45 properties! It is convenient that these properties could be grouped, so that the user is able to manage the properties per item. The attribute System.ComponentModel.Category should be used for this. The properties would then look something like this:

[Category("Item 1")]
public string TagCloudItem1Text { get; set; }
[Category("Item 1")]
public double TagCloudItem1Weight { get; set; }
[Category("Item 1")]
public string TagCloudItem1NavigateUrl { get; set; }
[Category("Item 2")]
public string TagCloudItem2Text { get; set; }
[Category("Item 2")]
public double TagCloudItem2Weight { get; set; }
[Category("Item 2")]
public string TagCloudItem2NavigateUrl { get; set; }
[Category("Item 3")]
public string TagCloudItem3Text { get; set; }
[Category("Item 3")]
public double TagCloudItem3Weight { get; set; }
[Category("Item 3")]
public string TagCloudItem3NavigateUrl { get; set; }

After creating a (large) number of properties, we have to write code that creates tagcloud items from them in the tagcloud. For managing the properties, we are going to write an Enum:

private enum TagCloudProperty
{
    Text,
    Weight,
    NavigateUrl
}

Then, we could write a method that dynamically gets all available properties in the class. This way, we do not have repeat the same logic over and over again. In a widget like the tagcloud, which potentially has an enormous amount of properties, this is the way to do it: the number and nature of the property are passed as parameters in the method, and by this information, the name of the property is constructed as a string, that is returned from the method.

private string GetTagCloudItemProperty(int i, TagCloudProperty property)
    {

    string tagCloudItemProperty;
    PropertyInfo currentProperty = this.GetType().GetProperty("TagCloudItem" + i + property);

    if (currentProperty.GetValue(this, null) != null)
    {
        tagCloudItemProperty = currentProperty.GetValue(this, null).ToString();
    }
    else
    {
        tagCloudItemProperty = string.Empty;
    }
    return tagCloudItemProperty;
}

From this helper method, we could write the method that populates the tagcloud. The method is basically a for-loop, that is set to iterate 21 times. This is one time more than the maximum number of items in the tagcloud. This value should be set according to your specific needs. First in the loop, the local variables are set with the GetTagCloudItemProperty() method. Remember that this method returns a string, so for the Weight property, the returned value should be converted to a double. Now, if no item is null, we could construct the RadTagCloudItem, which is in the Telerik.Web.UI namespace. Its constructor accepts the three parameter we just got from our properties. Add the item to the items collection of your TagCloud, and you are done. Enjoy your configurable tagcloud!

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    if (!Page.IsPostBack)
    {
        PopulateTagCloud();
    }
}

private void PopulateTagCloud()
{
    string itemText;
    double itemWeight;
    string itemNavigateUrl;

    for (int i = 1; i < 21; i++)
    {
        itemText = GetTagCloudItemProperty(i, TagCloudProperty.Text);
        itemWeight = Convert.ToDouble(GetTagCloudItemProperty(i, TagCloudProperty.Weight));
        itemNavigateUrl = GetTagCloudItemProperty(i, TagCloudProperty.NavigateUrl);

        if (!itemText.IsNullOrEmpty() && itemWeight != 0 && !itemNavigateUrl.IsNullOrEmpty())
        {
            RadTagCloudItem currentTagCloudItem = new RadTagCloudItem(itemText, itemWeight, itemNavigateUrl);

            RadTagCloud1.Items.Add(currentTagCloudItem);
        }
    }
}
Advertisements