ASP.NET Nested Repeater

This code is exactly usefull to embeding one repeater into another repeater control...this helps to display heirarchical data.

========= XML FILE ============

----- We have an xml file containing heirarchical data and we have to show output like - ----

Group Information

  • 1
    • Test 1
    • Test 2
  • 2
    • Test 3
  • 3
    • Test 4
    • Test 5

================================================

Create an xml file

<?xml version="1.0" encoding="utf-8" ?>

<Groups>

<category GroupId="1">

<Name>Test 1</Name>

<Name>Test 2</Name>

</category>

<category GroupId="2">

<Name>Test 3</Name>

</category>

<category GroupId="3">

<Name>Test 4</Name>

<Name>Test 5</Name>

</category>

</Groups>

=================================

next come to source view        == =========

add this following code --

<asp:Repeater id="CategoryRepeater" runat="server" OnItemDataBound="CategoryRepeater_ItemDataBound">

<HeaderTemplate>

<h2>Group Information</h2>

<ul>

</HeaderTemplate>

<FooterTemplate>

</ul>

</FooterTemplate>

<ItemTemplate>

<li><b><%# Eval("GroupID") %></b></li>

 

<%-- adding one more repeater within this item template --%>

 

<asp:Repeater id="PlayerRepeater" runat="server">

<HeaderTemplate>

<ul>

</HeaderTemplate>

<FooterTemplate>

</ul>

</FooterTemplate>

<ItemTemplate>

<li><%# Eval("Name_Text") %></li>

</ItemTemplate>

</asp:Repeater>

</ItemTemplate>

</asp:Repeater>

=====================================

now in the code behind ------------------

write following code ----------------

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DataSet ds = new DataSet();

ds.ReadXml(MapPath("./Group.xml"));

CategoryRepeater.DataSource = ds;

CategoryRepeater.DataBind();

}

}

protected void CategoryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

RepeaterItem item = e.Item;

if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)

{

Repeater PlayerRepeater = (Repeater)item.FindControl("PlayerRepeater");

DataRowView drv = (DataRowView)item.DataItem;

PlayerRepeater.DataSource = drv.CreateChildView("category_Name");

// here u can also bind the Child Repeater with DataTable like

//PlayerRepeater.DataSource = datatableName;

PlayerRepeater.DataBind();

}

}

}

By zafar iqbal   Popularity  (5293 Views)