/// <summary>
/// Inserts a collection of <see cref="ListItem"/> into a <see cref="System.Web.UI.WebControls.ListControl"/>.
/// Each item has a value of the name of the category plus ticks to indicate hierarchy between categories.
/// Each item has a value of the category's itemId.
/// If you have problems showing all Categories, you can try using <see cref="DisplayChildren(ListControl, int, int?)"/> on an instantiated ItemRelationship (see <see cref="Controls.ItemRelationships.UpdateAvailableItems"/> for an example).
/// </summary>
/// <param name="lc">The <see cref="System.Web.UI.WebControls.ListControl"/> into which the category options will be added.</param>
/// <param name="categoryId">The id of the parent category from which the hierarchy will start, or -1 if all categories.</param>
/// <param name="portalId">The id of the portal in which these categories reside.</param>
/// <param name="includeParentCategory">if set to <c>true</c> includes the parent category in the list, otherwise only shows the parent's children. This is ignored if the TopLevelCategory is selected.</param>
/// <param name="itemToExclude">An item which you want to exclude from the list (including its children). This is typically used to keep circular relationships from being possibe options.</param>
public static void DisplayCategoryHierarchy(ListControl lc, int categoryId, int portalId, bool includeParentCategory, int itemToExclude)
{
DataTable dt;
if (categoryId < 1)
{
dt = Category.GetCategoriesHierarchy(portalId);
// we ignore includeParentCategory if it is the TopLevelCategory
}
else
{
dt = Item.GetAllChildren(ItemType.Category.GetId(), categoryId, RelationshipType.ItemToParentCategory.GetId(), portalId).Tables[0];
if (includeParentCategory)
{
Category parentCategory = Category.GetCategory(categoryId, portalId);
if (parentCategory != null)
{
DataRow parentRow = dt.NewRow();
parentRow["ParentItemId"] = "-1";
parentRow["ItemId"] = categoryId;
parentRow["Name"] = parentCategory.Name;
dt.Rows.InsertAt(parentRow, 0);
}
}
}
TreeNode root = BuildHierarchy(dt);
FillListControl(root, lc, itemToExclude);
}