Binding Images to asp:repeater with Fluent API
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
>
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;
(Telerik.Sitefinity.Libraries.Model.Image)TypeDescriptor.GetProperties(e.Item.DataItem)[
"i"
].GetValue(e.Item.DataItem);
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;