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

Forums / Developing with Sitefinity / Sorting from News List Page

Sorting from News List Page

4 posts, 0 answered
  1. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    30 Jun 2010
    Link to this post
    Hello,

    I am looking for a way to allow the user to sort the list of news items by just clicking a link on the page.  I have tried many ways and just haven't been able to achieve this.  Any ideas or solutions?

    Thanks,
    Chris Morrow
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Jun 2010
    Link to this post
    Hello Chris Morrow,

    You can implement a custom user control that filters/sorts some of the content items

    For instance you can declare NewsView and RadComboBox inside the custom user control template


    <news:NewsView runat="server" ID="NewsView1" />
    <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true" />

    Then inside the code behind filter/sort NewsView list by using ContentFilterBuilder class

    protected void  Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCombo();
        }
        Fitler();
        this.NewsView1.SingleItemUrl = this.NewsView1.SingleItemUrl;
    }
    public void Fitler()
    {
      
        ContentFilterBuilder filter = new ContentFilterBuilder(this.NewsView1);
        filter.ClearFilter();
        if (RadComboBox1.SelectedItem.Text != "* Uncategorized *")
        {
            filter.AddFilter(new ContentFilterStatement("Category", RadComboBox1.SelectedItem.Text, ContentFilter.Condition.Equal));
        }
    }
    public void BindCombo()
    {
        var manager = new ContentManager("News");
        IList listofCategores = manager.GetCategories();
        RadComboBoxItem nullItem = new RadComboBoxItem("* Uncategorized *", string.Empty);
        RadComboBox1.Items.Add(nullItem);
        foreach (ICategory cat in listofCategores)
        {
            string val = cat.CategoryName;
            RadComboBoxItem item = new RadComboBoxItem(val, val);
            RadComboBox1.Items.Add(item);
        }
    }


    The better option is using custom control that inherits from NewsView

    sample code

    public class ContentViewCustomControl : NewsView
    {
        public ContentViewCustomControl()
        {
     
        }
     
        public override string ItemListTemplatePath
        {
            get
            {
                return "~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx";
            }
     
        }
     
     
     
        void BindCategories()
        {
            IList allcats = Manager.GetCategories();
            RadComboBoxItem nullItem = new RadComboBoxItem("* Uncategorized *", string.Empty);
            combo.Items.Add(nullItem);
            foreach (Telerik.Cms.Engine.ICategory cat in allcats)
            {
                string val = cat.CategoryName;
                RadComboBoxItem item = new RadComboBoxItem(val, val);
                combo.Items.Add(item);
     
            }
     
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
        }
     
        void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string rightOperant = e.Value;
            this.SelectedCategory = rightOperant;
             
            ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
            filterBuilder.ClearFilter();
            if (!String.IsNullOrEmpty(rightOperant) && combo.SelectedItem != null && e.Value != "* Uncategorized *")
            {
     
                ContentFilterStatement categoryFilter = new ContentFilterStatement("Category", rightOperant,
                                              ContentFilter.Condition.In, ContentFilter.JoinType.And);
                filterBuilder.AddFilter(categoryFilter);
            }
         
            this.RecreateChildControls();
        }
     
     
     
     
       
     
        protected override void CreateChildControls()
        {
     
            combo.AutoPostBack = true;
            this.BindCategories();
            combo.SelectedValue = this.SelectedCategory;
            
        }
     
     
     
        private ContentManager contentManager;
     
        private string SelectedCategory
        {
            get
            {
                return (string)ViewState["selCat"];
            }
            set
            {
                ViewState["selCat"] = value;
            }
        }
     
        protected virtual RadComboBox combo
        {
            get
            {
                return ListContainer.GetControl<RadComboBox>("combo1", true);
            }
        }
     
     
    }


    Kind 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. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    30 Jun 2010
    Link to this post
    Thank you for the reply.

    I actually have something similar to that for filtering the data. 

    What I am more trying to achieve is a way for them to be able to sort the filtered data.  Some way of changing the SortExpression.  I have it loading at first by my Title ASC, so if they clicked title or selected it out of a combobox I would want it to then sort by Title DESC.  I also have a rating metafield that I am using and would like them to be able to sort by this.  I've made a control that will store the desired SortExpression in the ViewState of the control.  But I haven't found a way to access this from my CustomNewsView.

    Thanks,
    Chris Morrow
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Jul 2010
    Link to this post
    Hi Chris Morrow,

    The code would be the same. The difference is that you have to override CreateDataSource method of the ContentView control and there call GetConent method which has 21 overloads. You could call GetContent(string sortExp) which will sort the Ilist that you have to return. After post back the ContentView control will show a sorted list.

    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
Register for webinar
4 posts, 0 answered