using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Web; |
using Telerik.Cms.Engine.WebControls.Admin; |
using Telerik.Web.UI; |
using System.Collections; |
using Telerik.Cms.Engine; |
|
/// <summary> |
/// Summary description for MyContentCategoriesField |
/// </summary> |
/// |
|
namespace Telerik.Samples |
{ |
public class MyContentCategoriesField : ContentCategoriesField |
{ |
public MyContentCategoriesField() |
{ |
} |
|
// set the template for CategoriesField.ascx |
public override string LayoutTemplatePath |
{ |
get |
{ |
if (string.IsNullOrEmpty(this.layoutTemplatePath)) |
this.LayoutTemplatePath = "~/Sitefinity_3_6_1870_standard_ExternalTemplates/Sitefinity/Admin/ControlTemplates/Generic_Content/CategoriesField.ascx"; |
return this.layoutTemplatePath; |
} |
set |
{ |
this.layoutTemplatePath = value; |
} |
} |
|
// override initialize controls |
protected override void InitializeControls(System.Web.UI.Control controlContainer) |
{ |
base.InitializeControls(controlContainer); |
this.CategoriesList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(CategoriesList_SelectedIndexChanged); |
this.CategoriesList.Items.Clear(); |
|
// get categories by CategoryName ASC |
IList categories = this.Manager.GetCategories(0, 0, "CategoryName ASC"); |
RadComboBoxItem liUncategorized = new RadComboBoxItem(); |
liUncategorized.Text = "-- uncategorized --"; |
liUncategorized.Value = string.Empty; |
if (String.IsNullOrEmpty(selectedCategory)) |
liUncategorized.Selected = true; |
this.CategoriesList.Items.Add(liUncategorized); |
AddCategory(Guid.Empty, categories); |
|
|
} |
|
/// <summary> |
/// Adds the category. |
/// </summary> |
/// <param name="parentId">The parent id.</param> |
/// <param name="categories">The categories.</param> |
private void AddCategory(Guid parentId, IList categories) |
{ |
foreach (ICategory category in categories) |
{ |
if (this.CategoriesList.FindItemByValue(category.ID.ToString()) == null && category.ParentCategoryID == parentId) |
{ |
RadComboBoxItem categoryItem = new RadComboBoxItem(category.CategoryName, category.CategoryName); |
int level = CalculateLevel(category, categories); |
|
if (level > 0) |
categoryItem.Style.Add("padding-left", string.Concat((10 * level), "px")); |
if (category.CategoryName == selectedCategory) |
categoryItem.Selected = true; |
|
this.CategoriesList.Items.Add(categoryItem); |
AddCategory(category.ID, categories); |
} |
} |
} |
|
/// <summary> |
/// Calculates the level. |
/// </summary> |
/// <param name="category">The category.</param> |
/// <param name="categories">The categories.</param> |
/// <returns></returns> |
private static int CalculateLevel(ICategory category, IList categories) |
{ |
int level = 0; |
while (category.ParentCategoryID != Guid.Empty) |
{ |
level++; |
foreach (ICategory category1 in categories) |
{ |
if (category.ParentCategoryID == category1.ID) |
{ |
category = category1; |
break; |
} |
} |
} |
return level; |
} |
|
void CategoriesList_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) |
{ |
//Controls.Clear(); |
RadComboBox combo = new RadComboBox(); |
if (combo.SelectedItem.Value != String.Empty) |
selectedCategory = combo.SelectedItem.Value; |
Controls.Add(combo); |
} |
private string selectedCategory; |
private string layoutTemplatePath; |
} |
} |