New Technique For Databinding Sitecore Items

Posted Wednesday, September 8, 2010 11:03 PM by Kevin

For a while now, I’ve been primarily using Sitecore sublayouts as my rendering-of-choice. The suite of controls available are hard to resist, and it’s easier for someone that doesn’t know XSLT or Sitecore to modify the sublayouts (they’re just user controls, after all). Recently, I’ve started data binding SItecore items to ASP.NET Repeater controls in my sublayouts in a new way using LINQ.

For example, let’s say we have a multilist field on the current item and we want to display a list of the linked items with thumbnails and short descriptions. Here’s how I might now build my Repeater for that:

<asp:Repeater id=“SeeMoreRepeater” runat=“server”>
        <ItemTemplate>
                <sc:Image runat=“server” Item=“<%# Container.DataItem %>” Field=“Thumbnail” />
                <div class=“header”>
                        <sc:Text runat=“server” Item=“<%# Container.DataItem %>” Field=“Title” />
                </div>
                <p>
                        <sc:FieldRenderer runat=“server” Item=“<%# Container.DataItem %>” Field=“ShortDescription” />
                </p>
        </ItemTemplate>
</asp:Repeater>

Code-behind looks like this:

MultiListField seeMoreField = Sitecore.Context.Item.Fields[“SeeMore”];
SeeMoreRepeater.DataSource = seeMoreField.TargetIDs.Select( id => Sitecore.Context.Database.GetItem(id) );
SeeMoreRepeater.DataBind();

This seems like a very clean way to build a Repeater control and a clean way to data bind in code-behind. Additionally, using the <sc:Image>, <sc:Text>, and <sc:FieldRenderer> controls enables the content to be edited when the page is viewed in Sitecore’s page editor.

What do you think?

Filed under: , ,

Comments

# Twitter Trackbacks for New Technique For Databinding Sitecore Items - Kevin Williams .NET and Stuff [dotnetmafia.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 New Technique For Databinding Sitecore Items - Kevin Williams .NET and Stuff         [dotnetmafia.com]        on Topsy.com

# re: New Technique For Databinding Sitecore Items

Thursday, September 9, 2010 12:11 PM by Alex Shyba

Hi Kevin,

I prefer setting the Item property of the FieldRenderers in OnItemDataBound event of the Repeater. This way your markup in ASCX becomes even cleaner as you don't need this:

Item=“<%# Container.DataItem %>”

# re: New Technique For Databinding Sitecore Items

Wednesday, October 6, 2010 3:20 PM by Matt Hovany

Alex,

That's how we always used to do it.  This way saves us the hassle of binding the ItemDataBound event,  casting the DataItem, finding the control by ID, casting the control to a field renderer, then setting the item.

There will be some situations that require the codebehind method, but i think this is a good way to just knock out a basic sublayout in a project that probably has many other similar repeaters.

# re: New Technique For Databinding Sitecore Items

Wednesday, October 6, 2010 3:26 PM by Matt Hovany

Also, instead of setting just IDs as the datasource, you could set Item[] as a datasource and get the same result with:

Item='<%# Eval("ID") %>'

# re: New Technique For Databinding Sitecore Items

Wednesday, October 6, 2010 7:14 PM by Kevin

Not sure if you're referring to my code or something Alex mentioned, but you might have missed the .Select() Linq query I'm using to turn the TargetIDs into a collection of Item's.

And hey, Matt! Good to see a co-worker found my blog. :)

# re: New Technique For Databinding Sitecore Items

Tuesday, July 5, 2011 7:40 AM by Akshay Sura

I agree with both methods. Depends on the need. I think the OnItemDataBound is especially useful if we need to cast the item, this was especially useful to me while doing the search results display.