I have a repeater wanting to display Categories and products. Category should appear once and products would appear the number of products i have. Below is my markup
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server"></asp:Label>
<asp:Label ID="lblProductName" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
I bind the products at page load
if (!Page.IsPostBack)
{
LoadData();
}
My codebehind
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Product p = (Product)e.Item.DataItem;
Label lblCategory = e.Item.FindControl("lblCategory") as Label;
Label lblProductName = e.Item.FindControl("lblProductName") as Label;
lblProductName.Text = p.ProductName;
lblCategory.Text = p.Category.CategoryName;
}
}
Everything works but my category text is repeated more than once (its shown the number of products i have). How could i display the category just once?
Edit
Cat 20 is Stationery
Cat 30 is Computer Items
Cat 40 is Toiletry
**Id CatId ProductName**
1 20 Pencil
1 20 Pen
1 30 Compact Disc
1 30 USB
1 30 Hard drive
1 40 Toothpaste
1 40 Toothbrush
I get my data in an
Iqueryable<Product> LoadData = myContext.GetProducts();