Hi I have to display hierarchical information (which has four levels) within a repeater. For this I decided to use the nested repeater control.
I found this article on MSDN, http://support.microsoft.com/kb/306154 which shows how to use nested repeaters for two levels of information.
Can someone please help me extend this to four levels?
A sample code would be much appriciated. Thank you.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
HTML CODE :
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<h1>
Repeater 1</h1>
<asp:Repeater ID="Repeater2" runat="server" onitemdatabound="Repeater2_ItemDataBound">
<ItemTemplate>
<h1>
Repeater 2
</h1>
<asp:Repeater ID="Repeater3" runat="server" onitemdatabound="Repeater3_ItemDataBound">
<ItemTemplate>
<h1>
Repeater 3
</h1>
<asp:Repeater ID="Repeater4" runat="server" onitemdatabound="Repeater4_ItemDataBound">
<ItemTemplate>
<h1>
Repeater 4
</h1>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
//Need to assign the Data in datatable
Repeater1.DataSource = dt1;
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
DataTable dt2 = new DataTable();
//Need to assign the Data in datatable
Repeater2.DataSource = dt2;
Repeater2.DataBind();
}
}
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater3 = (Repeater)(e.Item.FindControl("Repeater3"));
DataTable dt3 = new DataTable();
//Need to assign the Data in datatable
Repeater3.DataSource = dt3;
Repeater3.DataBind();
}
}
protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater4 = (Repeater)(e.Item.FindControl("Repeater4"));
DataTable dt4 = new DataTable();
//Need to assign the Data in datatable
Repeater4.DataSource = dt4;
Repeater4.DataBind();
}
}
}
Method 2
There are plenty of examples available on internet. Following are some of that.
http://www.codeproject.com/Tips/563919/Nested-Repeater-Controls-in-ASP-NET
http://support.microsoft.com/kb/326338
From below link you can download the code also.
http://everymanprogrammer.com/index.php/nested-repeaters-do-it-clean-and-simple-a-beginners-tutorial-part-2/
Method 3
Building on the first answer, instead of building your table in the ItemDataBound function, you can pass in your table data on Page_Load, set it to a ViewState variable, then retrieve it when binding:
private DataTable GetCachedDataTable(string strTable)
{
DataTable dtableCached = (DataTable)this.ViewState[strTableCache];
return dtableCached;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ViewState["TblTwo_Cache"] = null;
DataTable tblOne = new DataTable();
DataTable tblTwo = new DataTable();
myFunctionReturningTwoTables(ref tblOne, ref tblTwo);
// Bind the first one
if (tblOne != null)
{
// This first line assumes an <asp:Panel ID=pnlMain runat=server>
// tag is added in front of the Repeater1 tag in the ASPX markup, above,
// and an </asp:Panel> tag is after the last </asp:Repeater> tag
Repeater rptr = pnlMain.FindControl("Repeater1") as Repeater;
rptr.ItemDataBound += new RepeaterItemEventHandler(rptrItemDataBound);
rptr.DataSource = tblOne;
rptr.DataBind();
}
// Cache the 2nd (and others...) like this
if (tblTwo != null)
{
this.ViewState["TblTwo_Cache"] = tblTwo;
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptr2 = e.Item.FindControl("Repeater2") as Repeater;
if (rptr2 != null)
{
DataTable dt = new DataTable();
// Now, pull it out of cache
dt = GetCachedDataTable("TblTwo_Cache");
if (dt != null)
{
rptr2.DataSource = dt;
rptr2.DataBind();
}
}
}
}
Method 4
If you have Strongly Typed Data type, better to use ItemTypeproperty which is available in <asp:Repeater> control so that you can easily set the DataSource property of your nested <asp:Repeater> control to <%#Container.DataItem %> as follow same steps for each nested repeater.
code sample:
<asp:Repeater ID="associatedDataRepeater" runat="server">
<ItemTemplate>
<asp:Repeater runat="server" DataSource='<%#Container.DataItem %>'>
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0