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

Forums / Developing with Sitefinity / Thumbnail Image not showing for News custom control utilising inbuilt Newslist

Thumbnail Image not showing for News custom control utilising inbuilt Newslist

4 posts, 1 answered
  1. Pradip
    Pradip avatar
    1 posts
    Registered:
    31 Dec 2010
    31 Dec 2010
    Link to this post
    HI

    I have created a custom control to show news listing on homepage (first 2 news articles show up). This control accesses the inbuilt admin for News from Modules.

    This listing has Image title, Image and short description. We are able to successfully show the title and description but unable to show the image. Following is the code we have used, can you please help us find why we are not able to show the image.

    Thanks in advance.

    Pradip

    Newslisting.ascx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using App_Code;
    using System.Data;
    using System.Web.Security;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections;

    public partial class UserControls_UserControls_NewsListing : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    DataTable dt_news = GetNewsDataTable();
                    if (dt_news != null)
                    {
                        dlNews.DataSource = dt_news;
                        dlNews.DataBind();

                    }
                }
                catch (Exception ex)
                {
                    String error_message = ex.Message;
                }
            }
        }

      
        protected void dlNews_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "viewnews")
            {
                String text = e.CommandArgument.ToString().Trim();
                text = text.Replace(" ", "_");
                Response.Redirect("~/news-events.aspx?ID=" + text);
                Response.Write(ViewState["id"].ToString());
            }
        }

        private DataTable GetNewsDataTable()
        {
            DataTable dt_news = new DataTable();
            dt_news.Columns.Add("id", typeof(String));
            dt_news.Columns.Add("title", typeof(String));
            dt_news.Columns.Add("summary", typeof(String));
            dt_news.Columns.Add("content", typeof(String));
            dt_news.Columns.Add("date_created", typeof(String));
            dt_news.Columns.Add("link", typeof(String));
            dt_news.Columns.Add("image", typeof(String));


          
            DataTable dt_news_top2 = new DataTable();
            dt_news_top2.Columns.Add("id", typeof(String));
            dt_news_top2.Columns.Add("title", typeof(String));
            dt_news_top2.Columns.Add("summary", typeof(String));
            dt_news_top2.Columns.Add("link", typeof(String));
            dt_news_top2.Columns.Add("image", typeof(String));

            Telerik.News.NewsManager objNews = new Telerik.News.NewsManager("News");

            String news = String.Empty;
            IList listOfNewsItems = objNews.Content.GetContent();
            DataRow news_row = dt_news.NewRow();
            if (listOfNewsItems.Count > 0)
            {
                foreach (Telerik.Cms.Engine.IContent newsItem in listOfNewsItems)
                {
                    news_row = dt_news.NewRow();
                    news_row["id"] = newsItem.ID.ToString();
                    news_row["title"] = Convert.ToString(newsItem.GetMetaData("Title"));
                    news_row["summary"] = Convert.ToString(newsItem.GetMetaData("Summary"));
                    news_row["content"] = newsItem.Content.ToString();
                    news_row["date_created"] = newsItem.DateCreated.ToString();
                    news_row["link"] = "~/news-events/" + newsItem.UrlWithExtension;
                    news_row["image"] = newsItem.ThumbnailUrl;
                    dt_news.Rows.Add(news_row);
                }
            }
            if (dt_news.Rows.Count == 0)
            {
                return null;
            }
            else if (dt_news.Rows.Count > 0)
            {
                dt_news.DefaultView.Sort = "date_created desc";
                dt_news = dt_news.DefaultView.ToTable();

                for (int i = 0; i < 2; i++)
                {
                    dt_news_top2.ImportRow(dt_news.Rows[i]);
                }
            }
            return dt_news_top2;
        }
    }

    NewsListing.ascx


    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="NewsListing.ascx.cs" Inherits="UserControls_UserControls_NewsListing" %>
    <div>
        <asp:DataList ID="dlNews" DataKeyField="id" OnItemCommand="dlNews_ItemCommand" RepeatColumns="2"
            runat="server">
            <HeaderTemplate>
            </HeaderTemplate>
            <ItemTemplate>
                <div>
                    <asp:Image ID="imgNews" ImageUrl='<%# bind("image") %>' Width="222" Height="143"
                        runat="server" /><br />
                </div>
                <div>
                    <asp:HyperLink ID="HyperLink2" NavigateUrl='<%# bind("link") %>' Text='<%# bind("title") %>'
                        runat="server"></asp:HyperLink>
                    <br />
                    <br />
                    <asp:Label ID="summary" runat="server" Text='<%# bind("summary") %>'></asp:Label><br />
                    <br />
                    <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# bind("link") %>' runat="server">Read More</asp:HyperLink>
                </div>
            </ItemTemplate>
        </asp:DataList>
    </div>
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    31 Dec 2010
    Link to this post
    Hello Pradip,

    Use GetMetaData("Thumbnail") method of the IContent object. Also there is a public API that you can use instead of making database queries.

    Greetings,
    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
    Answered
  3. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    21 Apr 2011
    Link to this post
    Hi,
    I am trying to do the same thing, however when I call GetMetaData("Thumbnail") it returns "[Libraries]" followed by a guid.  How can I turn this into a valid URL to the image?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    27 Apr 2011
    Link to this post
    Hello Keith,

    Please check this post - resolving the url of dynamic links

    Greetings,
    Ivan Dimitrov
    the Telerik team

Register for webinar
4 posts, 1 answered