1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to sort a categorieslist

How to sort a categorieslist

6 posts, 0 answered
  1. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    12 Mar 2010
    Link to this post
    CategoriesList does not have a sort expression field. Is there an easy way to add one?  I have a list of years as categories and would prefer them to sort DESC
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    12 Mar 2010
    Link to this post
    Hello Drew Greenwell,

    Firstly, I would suggest that you should use CategoriesTreeView control instead of CategoriesList. The last one is considered as obsolete control.
    You need to create a custom control that inherits from CategoriesTree and override BindCategories(). There you can call Manager.GetCategories(0, 0, "CategoryName ASC");and populate the list in desired order.

    You can take a look at this blog post that shows how to create a custom CategoriesTree control - Filter CategoriesTree control by Blog

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    12 Mar 2010
    Link to this post
    Excellent, Thank you sir!
  4. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    12 Mar 2010
    Link to this post
    Thanks again for the help ivan, i got it up and running in no time. I have a question related to the CategoriesList being obsolete.  It seems that a ul layout like the CategoriesList is a far cleaner solution in my current scenario.  Can this type of layout be mimicked with the CategoriesTree or am i stuck with trying to support an obsolete control or rolling my own CategoriesList if I just want a ul of categories?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    12 Mar 2010
    Link to this post
    Hi Drew Greenwell,

    CategoriesList uses Repeater control, since CategoriesTree uses RadTreeView control. The RadTeeView also renders ul elements so, you could apply some styling over them.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    12 Mar 2010
    Link to this post
    ok thanks... it feels like I'm aiming a bazooka at a mousehole though


    -------------------------------EDIT

    I decided to use my own version. Not quite as robust, but perfect for my scenario. Just in case anyone happens across this post.. I have a parent category for news and then categories for each year. This displays each category from newest to oldest. You can swap sort order or change the listitem with a drop down in the property designer. I'm excluding root level categories, because all of the relevant categories in my News provider are sub categories of a company_news category.. it would probably be easier for some to do an IncludeParentID property, or just add a parent category selector style control.

    public partial class NewsCategoriesList : System.Web.UI.WebControls.CompositeControl
        {
            public enum Sort
            {
                DESCENDING = 0,
                ASCENDING = 1
            }
            public string ExcludeParentID {get; set;}
            public Sort SortOrder { get; set; }
            public string ListItemFormatString { get; set; }
     
            public NewsCategoriesList()
            {
                SortOrder = Sort.DESCENDING;
                ListItemFormatString = li;
                ExcludeParentID = Guid.Empty.ToString();
            }     
             
            private string[] sorts = new string[] { "DESC", "ASC" };
            private const string li = "<li><a href='/newsroom.aspx?Year={0}'>{0}</a></li>";
     
            protected override void CreateChildControls()
            {
                //Retrieve list of Categories for "News" Provider
                Telerik.News.NewsManager mgr = new Telerik.News.NewsManager("News");
                IList categories = mgr.Content.GetCategories(0, 0, "CategoryName " + sorts[(int)SortOrder]);
                 
                StringBuilder sb = new StringBuilder("<ul id='NewsCategories'>");
                for (int i = 0; (i <= categories.Count - 1); i++)
                {
                    ICategory cat = (ICategory)categories[i];
                    if (cat.ParentCategoryID.ToString() != ExcludeParentID.ToString())
                    {
                        sb.AppendLine(String.Format(ListItemFormatString, cat.CategoryName, cat.ID));
                    }
                }
                sb.AppendLine("</ul>");
                this.Controls.Add(new LiteralControl(sb.ToString()));
                 
            }
     
        }


Register for webinar
6 posts, 0 answered