ItemDataBound in a Nested Repeater

February 14, 2010

Something I struggled with early in my ASP.net development was how to handle ItemDataBound event (or any repeater event) when it was inside another repeater in the code behind logic. I am presenting my code here so that it may be of use to anyone else having the same problem.

Given MyPage.aspx


   

My Repeater

Given MyPage.aspx.vb


' **** Please note functions CategoriesDataLayer.GetList() And ItemsDataLayer.GetCategories(CategoryId)
' ****   are used for demonstration only.  You may use any collection of data (datatable, collection, etc)    

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      rptMyOuterRepeater.DataSource = CategoriesDataLayer.GetList()
      rptMyOuterRepeater.DataBind()
End Sub

Protected Sub rptMyOuterRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rptMyOuterRepeater.ItemDataBound
      ' Exit function if this is not in ItemTemplate
      If Not e.Item.ItemType = ListItemType.AlternatingItem AndAlso Not e.Item.ItemType = ListItemType.Item Then Exit Sub
      Dim rptMyInnerRepeater As Repeater = e.Item.FindControl("rptMyInnerRepeater")
      rptMyInnerRepeater.DataSource = ItemsDataLayer.GetItemsByCategory(e.Item.DataItem("CategoryId"))
      AddHandler rptMyInnerRepeater.ItemDataBound, AddressOf rptMyInnerRepeater_ItemDataBound
      rptMyInnerRepeater.DataBind()
End Sub

Protected Sub rptMyInnerRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
      ' Exit function if this is not in ItemTemplate
      If Not e.Item.ItemType = ListItemType.AlternatingItem AndAlso Not e.Item.ItemType = ListItemType.Item Then Exit Sub
      CType(e.Item.FindControl("ltlItem"), Literal).Text = e.Item.DataItem("ItemName")
End Sub
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

posted in Uncategorized by admin

Follow comments via the RSS Feed| Trackback URL

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org