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

Forums / General Discussions / A RadNewsRotator widget that show the news without relation betwwen news and images

A RadNewsRotator widget that show the news without relation betwwen news and images

7 posts, 0 answered
  1. Enrique Coronel
    Enrique Coronel avatar
    17 posts
    Registered:
    10 Jul 2008
    17 Feb 2011
    Link to this post
    Hi, actually i try modify the radNewsRotator control of this example: http://bit.ly/fcMIuR , but the code-behind is complex 
    I want to show the news without the relation between the title of the news and the title of an image. Could you help me please?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    18 Feb 2011
    Link to this post
    Hi Luis,

    You can get only the news items using the code below

    App.WorkWith().NewsItems().Where(n => n.Status == ContentLifecycleStatus.Live).Get().Take(this.NewsLimit).ToList()

    Then you can pass the list as a datasource of the rotator control.

    All the best,
    Ivan Dimitrov
    the Telerik team
  3. Enrique Coronel
    Enrique Coronel avatar
    17 posts
    Registered:
    10 Jul 2008
    18 Feb 2011
    Link to this post
    I tried with the code mentioned before but an error appeared 'Exception Details: System.NullReferenceException: Object reference not set to aninstance of an object.' in 
    Line38: NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);
    i searched in the methods of the API of the news module, but I did not work properly. Could you help me please?

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    21 Feb 2011
    Link to this post
    Hi Luis,

    Could you check whether e.Item.DataItem is null. You are getting null reference exception.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
  5. Enrique Coronel
    Enrique Coronel avatar
    17 posts
    Registered:
    10 Jul 2008
    21 Feb 2011
    Link to this post
    Hi, i don't found the code line in that the list took null values. I attach my code. Any suggestion?



        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using Telerik.Sitefinity.News.Model;
        using System.ComponentModel;
        using Telerik.Web.UI;
        using Telerik.Sitefinity.GenericContent.Model;
        using Telerik.Sitefinity.Web.DataResolving;
        using Telerik.Sitefinity.Pages.Model;
        using Telerik.Sitefinity;
        using Telerik.Sitefinity.Modules.News.Web.UI;
        using Telerik.Sitefinity.Modules.News;




        public partial class User_Controls_NewsRotator : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                NewsManager Manager = NewsManager.GetManager();


                this.RadRotator1.DataSource = Manager.GetNewsItems()
                    .Where(n => n.Status == ContentLifecycleStatus.Live).ToList();
     
                RadRotator1.ItemDataBound += new RadRotatorEventHandler(RadRotator1_ItemDataBound);
                RadRotator1.DataBind();
            }


            void RadRotator1_ItemDataBound(object sender, Telerik.Web.UI.RadRotatorEventArgs e)
            {
                var link = e.Item.FindControl("newsLink") as HyperLink;
                var title = e.Item.FindControl("newsTitle") as Label;
                var text = e.Item.FindControl("newsText") as Label;


                NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);        
            
                if (title != null) title.Text = newsItem.Title;
                if (text != null) text.Text = newsItem.Summary;
                if (link != null && TargetNewsPage != null) link.NavigateUrl = DataResolver.Resolve(newsItem, "URL", null, TargetNewsPage.Id.ToString());
            }


            protected PageNode TargetNewsPage
            {
                get
                {
                    if (this.targetNewsPage == null)
                    {
                        this.targetNewsPage = App.WorkWith().Pages()
                                    .Where(p => p.Page != null &&
                                                p.Page.Controls.Where(c => c.ObjectType.StartsWith(typeof(NewsView).FullName)).Count() > 0)
                                    .Get().FirstOrDefault();


                    } //it can still be null in case there is no page with a NewsView on it
                    return this.targetNewsPage;
                }


            }


            private PageNode targetNewsPage;


            public virtual int NewsLimit
            {
                get
                {
                    return this.newsLimit;
                }
                set
                {
                    this.newsLimit = value;
                }
            }
            private int newsLimit = 8;
        }

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    25 Feb 2011
    Link to this post
    Hello Luis,

    I am not sure what you are trying to do with this line in your code


    NewsItem newsItem = (NewsItem)TypeDescriptor.GetProperties(e.Item.DataItem)["NewsItem"].GetValue(e.Item.DataItem);

    where the error is actually thrown.

    TypeDescriptor.GetProperties returns the collection of properties on a component or type. There is no property NewsItem which you pass as a key.
    The data item of the repeater is NewsItem so you can have only this

    var newsItem = e.Item.DataItem as NewsItem;

    If the e.Item.DataItem is null then the repeater datasource is empty and there are no items in it.

    Regards,
    Ivan Dimitrov
    the Telerik team
  7. omhoge
    omhoge avatar
    5 posts
    Registered:
    12 Jan 2007
    14 Jun in reply to Ivan Dimitrov
    Link to this post

    What he is trying to do in that line of code is what Sitefinity tells us to do. That line of code, in fact that whole method is from your OWN sitefinity documentation:

    http://docs.sitefinity.com/newsrotator-widget-implement-the-features-of-the-user-control

    I've been having a heck of a time getting that widget to work as well, and have reported errors in this documentation before.  I really hope you will correct it.

7 posts, 0 answered