Nested Repeaters using onitemdatabound


/ Published in: C#
Save to your folder(s)

Used to create a tabbed structure with accordions inside. NOTE: OnItemDataBound is called for each section, so you may want to filter on the proper section:

ListItemType lt = e.Item.ItemType;
if(lt == ListItemType.Item)....


Copy this code and paste it in your HTML
  1. private void rptMonthSummary_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  2. {
  3. ListItemType lt = e.Item.ItemType;
  4. if(lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
  5. {
  6. DataRowView dv = e.Item.DataItem as DataRowView;
  7. if(dv != null)
  8. {
  9. Repeater nestedRepeater = e.Item.FindControl("rptDailySummary") as Repeater;
  10. if (nestedRepeater != null)
  11. {
  12. nestedRepeater.DataSource = dv.CreateChildView("yearMonth");
  13. nestedRepeater.DataBind();
  14. }
  15. }
  16. }
  17. }
  18.  
  19.  
  20. //here's a full code-behind that uses a strongly typed list of results to filter:
  21.  
  22. public List<ProductWithType> ProductList = new List<ProductWithType>();
  23.  
  24. protected void Page_Load(object sender, EventArgs e)
  25. {
  26. GetProducts();
  27. }
  28.  
  29. private void GetProducts()
  30. {
  31. using (FTJEntities context = new FTJEntities())
  32. {
  33. ProductList = (from p in context.Products
  34. orderby p.ProductType.Name, p.Name
  35. select new ProductWithType()
  36. {
  37. ProductID = p.ProductID,
  38. ProductTypeID = p.ProductTypeID,
  39. Name = p.Name,
  40. Type = p.ProductType.Name,
  41. ShortDescription = p.ShortDescription
  42. }).ToList<ProductWithType>();
  43. rptProductTypes.DataSource = ProductList;
  44. rptProductTypes.DataBind();
  45. }
  46. }
  47.  
  48. public void rptProductTypes_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  49. {
  50. ListItemType lt = e.Item.ItemType;
  51.  
  52. if (!(lt == ListItemType.Item || lt == ListItemType.AlternatingItem)) return; //only interested in the item/alternatingitem databind calls to this method, not the header, footer, etc.
  53.  
  54. ProductWithType product = e.Item.DataItem as ProductWithType; //get the current row being databound
  55.  
  56. Repeater rptProducts = e.Item.FindControl("rptProducts") as Repeater; //get the nested control
  57. rptProducts.DataSource = from p in ProductList where p.ProductTypeID == product.ProductTypeID select p;
  58. rptProducts.DataBind();
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// Used so result set above can be strongly typed.
  64. /// </summary>
  65. public class ProductWithType
  66. {
  67. public int ProductID { get; set; }
  68. public int ProductTypeID { get; set; }
  69. public string Name { get; set; }
  70. public string Type { get; set; }
  71. public string ShortDescription { get; set; }
  72. }

URL: http://idunno.org/archive/2004/10/30/145.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.