Strongly typed repeaters has been around for a little while in .net now and I really like how it makes the front end code in my repeaters look less messy when I am using them in a Sitecore solution.
Consider the following snippet of code:
<asp:Repeater ID="repProductList" runat="server"> <ItemTemplate> <h2> <a href="<%# Sitecore.Links.LinkManager.GetItemUrl(Container.DataItem as Sitecore.Data.Items.Item) %>"> <sc:Text ID="header" Field="Header" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" runat="server" /> </a> </h2> <sc:Text ID="content" Field="Product Overview" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" runat="server" /> </ItemTemplate> </asp:Repeater>
In this pretty simple example I am only casting the Container.DataItem to a Sitecore item.
Now do the same thing with a typed repeater, note the “ItemType” parameter in the repeater tag:
<asp:Repeater ID="repProductList" ItemType="Sitecore.Data.Items.Item" runat="server"> <ItemTemplate> <h2> <a href="<%# Sitecore.Links.LinkManager.GetItemUrl(Item) %>"> <sc:Text ID="header" Field="Header" Item="<%# Item %>" runat="server" /> </a> </h2> <sc:Text ID="content" Field="Product Overview" Item="<%# Item %>" runat="server" /> </ItemTemplate> </asp:Repeater>
- No more casting of the repeater datasource item to a Sitecore item. (Costly?)
- No need for handling ItemDataBound events where you iterate to find your controls and then set the datasource
- You have access to the whole Item object in the repeater and can get any properties you’ll like
The same thing goes when we are building a repeater returning results from a search using Sitecore Search API, you can set your repeaters ItemType to the generic SearchResultItem provided by Sitecore or your own derived class. More on that in another post.