Strongly typed access to your page data objects inside ItemTemplate
Posted on May 10, 2012 by Frederik Vig in ASP.NET, EPiServerHere’s a little quick tip on how I usually set up my code to get strongly typed access to my page types inside Repeaters, ListViews, PageLists etc.
I use PageTypeBuilder in all my projects so I’m going to use it in the examples here as well, but you could just use the regular PageData objects instead (with a little more code).
<asp:Repeater runat="server" ID="rptNews"> <HeaderTemplate> <div class="entries"> </HeaderTemplate> <ItemTemplate> <%# SetItem(Container.DataItem) %> <div class="entry"> <a href="<%# NewsPage.LinkURL %>"> <img src="<%# NewsPage.ListImage %>" runat="server" visible="<%# NewsPage.ListImage.IsNotNullOrEmpty() %>" /> <h2><%# NewsPage.Heading %></h2> </a> <div class="date"> <%# NewsItem.StartPublish.ToString("dd.MM.yyyy") %></div> </div> </ItemTemplate> <FooterTemplate> </div> </FooterTemplate> </asp:Repeater> |
The thing to note here is that I’m calling the method SetItem(), which sets the current news page for me.
protected string SetItem(object dataItem) { this.NewsPage = dataItem as StandardPage; return string.Empty; } |
NewsPage is just a auto property of type StandardPage.
Update: 13.05.2012
Thanks to Jon’s comment, we can simplify this code even more. Instead of using the SetItem method we can use the GetDataItem() method from the Page class.
protected StandardPage NewsPage { get { return Page.GetDataItem() as StandardPage; }} |
This gives me strongly typed access to my objects. In ASP.NET 4.5 this will be even simpler since strongly typed access (model binding) will be built into controls like the Repeater, take a look at Scott Guthrie’s post if you’re curious about ASP.NET 4.5.
Petter says:
Post Author May 10, 2012 at 15:32Nice!
Have found myself wanting this a couple of times.
Thanks for sharing!
Andreas Nicolaisen says:
Post Author May 10, 2012 at 16:12Isn’t it just easier to write <img src="” ?
Then you do not need any “magic” method in codebehind. You should of course filter the datasource to make sure it contains only the types you want.
Andreas Nicolaisen says:
Post Author May 10, 2012 at 16:15Hmm, no fancy tags allowed in this commentfield I guess:)
It should say in the src tag: ((NewsPage)Container.DataItem).ListImage
Frederik Vig says:
Post Author May 10, 2012 at 16:37@Andreas sure, but when you have a lot of them it can get messy, performance wise it is also faster to cast it once and assign to a property instead of casting each time you need to access it.
Jon Wiberg says:
Post Author May 11, 2012 at 11:21I have used a similar approach, and have some comments/feedback 🙂
Why not make use of your property this.NewsPage directly?
Page.GetDataItem gives access to the currently scoped dataitem directly, therfore no need for a setmethod (SetItem)
MSDN – Page.GetDataItem “Gets the data item at the top of the data-binding context stack.”
http://msdn.microsoft.com/en-us/library/system.web.ui.page.getdataitem(v=vs.90).aspx
I have described this on the following sites
(Google auto translated)
* http://translate.google.se/translate?sl=sv&tl=en&js=n&prev=_t&hl=sv&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.pellesoft.se%2Fcommunicate%2Fforum%2Fview.aspx%3Fmsgid%3D276467%26forumid%3D10%26sum%3D0&act=url
Indirect in this code sample
* http://odeto.net/post/The-great-Form-lie.aspx
Frederik Vig says:
Post Author May 13, 2012 at 13:51@Jon Nice! Didn’t know about the Page.GetDataItem() method.
Erik Nordin says:
Post Author May 15, 2012 at 15:02Nice tip Frederik (and Jon)! 🙂