Return to Snippet

Revision: 35041
at December 17, 2010 07:16 by housecor


Updated Code
private void rptMonthSummary_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
  ListItemType lt = e.Item.ItemType;
  if(lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
  {
    DataRowView dv = e.Item.DataItem as DataRowView;
    if(dv != null)
    {
      Repeater nestedRepeater = e.Item.FindControl("rptDailySummary") as Repeater;
      if (nestedRepeater != null)
      {
        nestedRepeater.DataSource = dv.CreateChildView("yearMonth");
        nestedRepeater.DataBind();
      }
    }
  }
}


//here's a full code-behind that uses a strongly typed list of results to filter:

		public List<ProductWithType> ProductList = new List<ProductWithType>();

        protected void Page_Load(object sender, EventArgs e)
        {
			GetProducts();
        }

		private void GetProducts()
		{
			using (FTJEntities context = new FTJEntities())
			{
				ProductList = (from p in context.Products
							   orderby p.ProductType.Name, p.Name
							select new ProductWithType() 
							{
								ProductID = p.ProductID,
								ProductTypeID = p.ProductTypeID,
								Name = p.Name,
								Type = p.ProductType.Name,
								ShortDescription = p.ShortDescription
							}).ToList<ProductWithType>();
				rptProductTypes.DataSource = ProductList;
				rptProductTypes.DataBind();
			}
		}

		public void rptProductTypes_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			ListItemType lt = e.Item.ItemType;

			if (!(lt == ListItemType.Item || lt == ListItemType.AlternatingItem)) return; //only interested in the item/alternatingitem databind calls to this method, not the header, footer, etc.
		
			ProductWithType product = e.Item.DataItem as ProductWithType; //get the current row being databound

			Repeater rptProducts = e.Item.FindControl("rptProducts") as Repeater; //get the nested control
			rptProducts.DataSource = from p in ProductList where p.ProductTypeID == product.ProductTypeID select p;
			rptProducts.DataBind();
		}
    }

	/// <summary>
	/// Used so result set above can be strongly typed.
	/// </summary>
	public class ProductWithType
	{
		public int ProductID { get; set; }
		public int ProductTypeID { get; set; }
		public string Name { get; set; }
		public string Type { get; set; }
		public string ShortDescription { get; set; }
	}

Revision: 35040
at November 10, 2010 14:01 by housecor


Updated Code
private void rptMonthSummary_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
  ListItemType lt = e.Item.ItemType;
  if(lt == ListItemType.Item || lt == ListItemType.AlternatingItem)
  {
    DataRowView dv = e.Item.DataItem as DataRowView;
    if(dv != null)
    {
      Repeater nestedRepeater = e.Item.FindControl("rptDailySummary") as Repeater;
      if (nestedRepeater != null)
      {
        nestedRepeater.DataSource = dv.CreateChildView("yearMonth");
        nestedRepeater.DataBind();
      }
    }
  }
}

Revision: 35039
at November 1, 2010 13:03 by housecor


Initial Code
protected void rptNews_OnItemDataBound(object src, RepeaterItemEventArgs e)
		{
			int caseIDAboutToBePrinted = (int)DataBinder.Eval(e.Item.DataItem, "case_ID");
			if (caseIDAboutToBePrinted != LastCaseIDPrinted)
			{ //so about to print a new case
				if (LastCaseIDPrinted != null) //so about to print a new case and a previous case has been printed, so need to close previous div
				{
					((Literal)e.Item.FindControl("litClosePreviousCaseDiv")).Text = "</div>";
				}
				((Literal)e.Item.FindControl("litOpenCaseDiv")).Text = "<div class=\"accordion\" id=\"case" + caseIDAboutToBePrinted + "\">";
			}
			LastCaseIDPrinted = caseIDAboutToBePrinted;

			//now close the final div if about to print last item.
			if (e.Item.ItemIndex == NumNewsItems - 1)
			{
				((Literal)e.Item.FindControl("litCloseCaseDiv")).Text = "</div>";
			}
			BindAttachedFiles(e);
		}

//and in the front end:
		<asp:Repeater ID="rptNews" runat="server" OnItemDataBound="rptNews_OnItemDataBound">
			<ItemTemplate>
				<asp:Literal ID="litClosePreviousCaseDiv" runat="server"></asp:Literal>
				<asp:Literal ID="litOpenCaseDiv" runat="server"/>
				<h3><a href="#"><%#DataBinder.Eval(Container.DataItem, "Created")%> - <%#DataBinder.Eval(Container.DataItem, "Title")%></a></h3>
				<div>
					<%#DataBinder.Eval(Container.DataItem, "Body")%>
					<asp:Repeater ID="rptFiles" runat="server">
						<HeaderTemplate>
							<h3>Attachments</h3>
							<ul style="list-style-type:none;">
						</HeaderTemplate>
						<ItemTemplate>
							<li>
								<img src="../App_Themes/Theme1/Images/<%#DataBinder.Eval(Container.DataItem, "t.Icon")%>" alt="<%#DataBinder.Eval(Container.DataItem, "t.TypeName")%>" title="<%#DataBinder.Eval(Container.DataItem, "t.TypeName")%>" /> 
								<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "/News/GetFile.ashx?ID=" + DataBinder.Eval(Container.DataItem, "f.CaseNewsFileID")%>' Text='<%#DataBinder.Eval(Container.DataItem, "f.Filename")%>'></asp:HyperLink>
							</li>
						</ItemTemplate>
						<FooterTemplate></ul></FooterTemplate>
					</asp:Repeater>
				</div>
				<asp:Literal ID="litCloseCaseDiv" runat="server"></asp:Literal>
			</ItemTemplate>
		</asp:Repeater>

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

Initial Description
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)....

Initial Title
Nested Repeaters using onitemdatabound

Initial Tags


Initial Language
C#