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

Forums / Bugs & Issues / News Randomize items issue

News Randomize items issue

8 posts, 0 answered
  1. Menderes Demir
    Menderes Demir avatar
    7 posts
    Registered:
    26 Mar 2010
    01 Jul 2010
    Link to this post
    Hi,

    The website I'm currently working on, has custom newscontrols. We want to that one of these controls randomize its news items, see current codes below.

    Help will be appreciated.

    With kind regards,
    Menderes Demir

    listReferenties.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="listReferenties.ascx.cs" Inherits="CustomNewsControl" %>
    <%@ Register Assembly="Telerik.News"  Namespace="Telerik.News.WebControls" TagPrefix="news" %>
      
      
    <div class="item-ref">
        <div class="item-bg">
              <h2>Referenties</h2>
              <news:NewsView ID="nv1"  runat="server"ItemListTemplatePath="/Sitefinity/ControlTemplates/News/ListPageMaster.ascx"SingleItemTemplatePath="/Sitefinity/ControlTemplates/News/ListPageDetails.ascx"FilterExpression="Category = Referenties"  AllowPaging="false" MaximumListCount="3"LimitListCount="true"  />
        </div>   
    </div>
    <div class="item-bottom"></div>

    listReferenties.ascx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Engine;
    using MyReferenties;
     
    public partial class CustomNewsControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
     
        public Telerik.News.WebControls.NewsView NewsViewControlProperties
        {
     
     
            get
            {
                return nv1;
            }
            set
            {
                this.nv1 = value;
            }
     
        }
     
        public string SingleUrl
        {
     
            get
            {
                return this.nv1.SingleItemUrl;
            }
            set
            {
                this.nv1.SingleItemUrl = value;
            }
        }
     
     
    }

    listReferenties.cs (App_Code folder)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.News.WebControls;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Web;
    using Telerik.Cms.Web.UI;
    using Telerik.Cms.Engine.ContentViewFiltering;
    using Telerik.Libraries;
    using System.ComponentModel;
     
    namespace MyReferenties
    {
        /// <summary>
        /// Summary description for NewsViewCustomControl
        /// </summary>
        public class CustomNewsControl : NewsView
        {
     
     
     
            public override string ItemListTemplatePath
            {
                get
                {
                    return "~/Sitefinity/ControlTemplates/News/ListPageMaster.ascx";
                }
                set
                {
                    ItemListTemplatePath = value;
                }
            }
     
            public override string SingleItemTemplatePath
            {
                get
                {
                    return "~/Sitefinity/ControlTemplates/News/ListPageDetails.ascx";
                }
                set
                {
                    SingleItemTemplatePath = value;
                }
            }
     
            public override Type LocalizationAssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.LocalizationAssemblyInfo = value;
                }
            }
     
            public override Type AssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.AssemblyInfo = value;
                }
            }
     
     
            protected override System.Collections.IList CreateDataSource()
            {
     
                IList<IContent> cntList = new List<IContent>();
                System.Collections.IList list = Manager.GetContent();
                foreach (IContent cnt in list)
                {
                    cntList.Add(cnt);
     
                }
                Random rnd = new Random();
                var randomList = from item in cntList
                                 orderby rnd.Next()
                                 select item;
                List<IContent> qList = randomList.ToList();
     
                return qList;
     
            }
     
        }
    }

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    01 Jul 2010
    Link to this post
    Hello Menderes Demir,

    Register CustomNewsControl  in you web.config  and use it instead of the user control you have pasted.

    <toolboxControls
       <clear />
        <add name="CustomNewsControl" section="Custom" type="CustomNewsControl, App_Code" />

    You will be able to drag/drop the control from controls toolbox when you edit a page

    All the best,
    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. Menderes Demir
    Menderes Demir avatar
    7 posts
    Registered:
    26 Mar 2010
    01 Jul 2010
    Link to this post
    Hi Ivan,

    Thank you for your reply! It's working now except the property's I've used are not being "set", they are being shown in "Advanced" section of the control in page edit mode but they are not actually working. But when I delete the IList code they work:
        protected override System.Collections.IList CreateDataSource()
        {
     
            IList<IContent> cntList = new List<IContent>();
            System.Collections.IList list = Manager.GetContent();
            foreach (IContent cnt in list)
            {
                cntList.Add(cnt);
     
            }
            Random rnd = new Random();
            var randomList = from item in cntList
                             orderby rnd.Next()
                             select item;
            List<IContent> qList = randomList.ToList();
     
            return qList;
     
        }

    Somehow the IList code overrides the propery's I have set (full code):
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.News.WebControls;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Web;
    using Telerik.Cms.Web.UI;
    using Telerik.Cms.Engine.ContentViewFiltering;
    using Telerik.Libraries;
    using System.ComponentModel;
     
     
     
        public class CustomNewsControl : NewsView
        {
     
     
            protected override System.Collections.IList CreateDataSource()
            {
     
                IList<IContent> cntList = new List<IContent>();
                System.Collections.IList list = Manager.GetContent();
                foreach (IContent cnt in list)
                {
                    cntList.Add(cnt);
     
                }
                Random rnd = new Random();
                var randomList = from item in cntList
                                 orderby rnd.Next()
                                 select item;
                List<IContent> qList = randomList.ToList();
     
                return qList;
     
            }
     
     
            public override string FilterExpression
            {
                get
                {
                    return "Category = Referenties";
                }
                set
                {
                    base.FilterExpression = value;
                }
            }
     
            public override bool AllowPaging
            {
                get
                {
                    return false;
                }
                set
                {
                    base.AllowPaging = value;
                }
            }
     
            public override int MaximumListCount
            {
                get
                {
                    return 3;
                }
                set
                {
                    base.MaximumListCount = value;
                }
            }
     
            public override bool LimitListCount
            {
                get
                {
                    return true;
                }
                set
                {
                    base.LimitListCount = value;
                }
            }
     
            public override string ItemListTemplatePath
            {
                get
                {
                    return "~/Sitefinity/UserControls/ListPageMasterReferenties.ascx";
                }
                set
                {
                    base.ItemListTemplatePath = value;
                }
     
            }
     
            public override string SingleItemTemplatePath
            {
                get
                {
                    return "~/Sitefinity/ControlTemplates/News/ListPageDetails.ascx";
                }
                set
                {
                    base.SingleItemTemplatePath = value;
                }
            }
     
     
            public override Type LocalizationAssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.LocalizationAssemblyInfo = value;
                }
            }
     
            public override Type AssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.AssemblyInfo = value;
                }
            }
     
        }


    Thanks and with kind regards,
    Menderes demir
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    02 Jul 2010
    Link to this post
    Hi Menderes Demir,

    Modify CrateDataSource() as shown below

    protected override System.Collections.IList CreateDataSource()
      {
     
          IList<IContent> cntList = new List<IContent>();
          List<IContent> qList = null;
          ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
          if (filterBuilder.IsFilterValid)
          {
              IList items = Manager.GetContent(GetContentStartIndex(), GetPageSize(), SortExpression,
                                        filterBuilder.ParseTagFilter(), ContentStatus.Published, null,
                                        filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter());
     
     
     
              foreach (IContent cnt in items)
              {
                  cntList.Add(cnt);
     
              }
              Random rnd = new Random();
              var randomList = from item in cntList
                               orderby rnd.Next()
                               select item;
     
              qList = randomList.ToList();
          }
          
          return qList;


    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
  5. Menderes Demir
    Menderes Demir avatar
    7 posts
    Registered:
    26 Mar 2010
    05 Jul 2010
    Link to this post
    Hi Ivan,

    Thank you for your reply. After adding the code to enable the other codes to run properly, an error showed up.
    Any help will be appreciated.

    With kind regards,
    Menderes Demir

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0305: Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments

    Source Error:

    Line 27:       if (filterBuilder.IsFilterValid)
    Line 28:       {
    Line 29:           IList items = Manager.GetContent(GetContentStartIndex(), GetPageSize(), SortExpression,
    Line 30:                                     filterBuilder.ParseTagFilter(), ContentStatus.Published, null,
    Line 31:                                     filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter());

    Source File: d:\Program Files\telerik\Sitefinity3.7\WebSites\stateview.niolite.net\App_Code\listReferenties.cs    Line: 29




    My current code with the changed IList code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.News.WebControls;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Web;
    using Telerik.Cms.Web.UI;
    using Telerik.Cms.Engine.ContentViewFiltering;
    using Telerik.Libraries;
    using System.ComponentModel;
      
      
      
        public class CustomNewsControl : NewsView
        {
      
      
            protected override System.Collections.IList CreateDataSource()
            {
      
              IList<IContent> cntList = new List<IContent>();
              List<IContent> qList = null;
              ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this);
              if (filterBuilder.IsFilterValid)
              {
                 IList items = Manager.GetContent(GetContentStartIndex(), GetPageSize(), SortExpression,
                                        filterBuilder.ParseTagFilter(), ContentStatus.Published, null,
                                        filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter());
      
      
      
              foreach (IContent cnt in items)
              {
                  cntList.Add(cnt);
      
              }
              Random rnd = new Random();
              var randomList = from item in cntList
                               orderby rnd.Next()
                               select item;
      
              qList = randomList.ToList();
          }
           
          return qList;
      
            }
      
      
            public override string FilterExpression
            {
                get
                {
                    return "Category = Referenties";
                }
                set
                {
                    base.FilterExpression = value;
                }
            }
      
            public override bool AllowPaging
            {
                get
                {
                    return false;
                }
                set
                {
                    base.AllowPaging = value;
                }
            }
      
            public override int MaximumListCount
            {
                get
                {
                    return 3;
                }
                set
                {
                    base.MaximumListCount = value;
                }
            }
      
            public override bool LimitListCount
            {
                get
                {
                    return true;
                }
                set
                {
                    base.LimitListCount = value;
                }
            }
      
            public override string ItemListTemplatePath
            {
                get
                {
                    return "~/Sitefinity/UserControls/ListPageMasterReferenties.ascx";
                }
                set
                {
                    base.ItemListTemplatePath = value;
                }
      
            }
      
            public override string SingleItemTemplatePath
            {
                get
                {
                    return "~/Sitefinity/ControlTemplates/News/ListPageDetails.ascx";
                }
                set
                {
                    base.SingleItemTemplatePath = value;
                }
            }
      
      
            public override Type LocalizationAssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.LocalizationAssemblyInfo = value;
                }
            }
      
            public override Type AssemblyInfo
            {
                get
                {
                    return this.GetType();
                }
                set
                {
                    base.AssemblyInfo = value;
                }
            }
      
        }
  6. Menderes Demir
    Menderes Demir avatar
    7 posts
    Registered:
    26 Mar 2010
    05 Jul 2010
    Link to this post
    Hi Ivan,

    After doing some research I found out that I was missing:
    using system.collections;

    problem is now solved.

    With kind regards,
    Menderes Demir
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    05 Jul 2010
    Link to this post
    Hi Menderes Demir,

    Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments is a general ASP.NET error. You have not added the needed references to get the code running properly. Use System.Collections.IList

    System.Collections.IList items = Manager.GetContent(GetContentStartIndex(), GetPageSize(), SortExpression, filterBuilder.ParseTagFilter(), ContentStatus.Published, null, filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter());


    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
  8. Menderes Demir
    Menderes Demir avatar
    7 posts
    Registered:
    26 Mar 2010
    05 Jul 2010
    Link to this post
    Hi Ivan,

    Thanks for correcting.

    Because I have chosen to show the last 3 items with MaximumListCount the list randomizes the last 3 items. The list needs to randomize the whole list and show everytime only 3 items.

    Any help will appreciated.

    With kind regards,
    Menderes Demir
Register for webinar
8 posts, 0 answered