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

Forums / Sitefinity SDK / Binding Images to asp:repeater with Fluent API

Binding Images to asp:repeater with Fluent API

2 posts, 0 answered
  1. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    09 Dec 2010
    Link to this post
    I wanted to see how I could implement a portfolio gallery using jQuery.  I was thinking that I can take a repeater and generate all the markup that I will need for the jQuery and use the API to pull the data for me.  I am trying to emulate some of the code in the NewsRotator widget example, but an running into trouble with an "Object reference not set to an instance of an object" error.

    1.) I created a user control.
    2.) I registered the user control
    3.) I added some test markup and code behind and am getting the error.

    Here is my user control markup
    <asp:Repeater ID="PortfolioRepeater" runat="server">
       <HeaderTemplate><ul></HeaderTemplate>
       <ItemTemplate>
           <li><asp:Image ID="PortfolioImage" runat="server" /></li>
       </ItemTemplate>
       <FooterTemplate></ul></FooterTemplate>
    </asp:Repeater>

    Here is my user control code behind
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.GenericContent.Model;
    using System.ComponentModel;
     
    namespace SitefinityWebApp.UserControls
    {
        public partial class CustomPortfolio : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                PortfolioRepeater.DataSource = App.WorkWith().Images().Get().ToList().Where(i => i.Parent.Title == "Portfolio" &&
                                                    i.Status == ContentLifecycleStatus.Live);
     
                PortfolioRepeater.ItemDataBound += new RepeaterItemEventHandler(PortfolioRepeater_ItemDataBound);
                PortfolioRepeater.DataBind();
            }
     
            void PortfolioRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                var image = e.Item.FindControl("PortfolioImage") as Image;
     
                Telerik.Sitefinity.Libraries.Model.Image portImage =
                    (Telerik.Sitefinity.Libraries.Model.Image)TypeDescriptor.GetProperties(e.Item.DataItem)["i"].GetValue(e.Item.DataItem);
     
                if (image != null) image.ImageUrl = portImage.MediaUrl;
            }  
        }
    }

    I am pretty sure my problem is in the ItemDataBound event where I am creating the portImage object.  I am guessing I need something else in place of the "i" for this line I am pretty sure my problem is in the ItemDataBound event where I am creating the portImage object.  I am guessing I need something else in place of the "i" for this line.
    (Telerik.Sitefinity.Libraries.Model.Image)TypeDescriptor.GetProperties(e.Item.DataItem)["i"].GetValue(e.Item.DataItem);

    I am very new to the API so suggestions on what I am trying to do or a better approach are welcome.

    Thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Dec 2010
    Link to this post
    Hi Stacey,

    You are getting the error, because you are using an object that is "null" in the current context. You can get the image directly from the data item

    void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
           {
               // check whether you are in item template of the repater
     
                   var image = e.Item.DataItem as Telerik.Sitefinity.Libraries.Model.Image;
                
           }



    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
2 posts, 0 answered