Introduction
This article is usefull to show hierarchical data using repeater control.
Description
Basically this project shows how to embed one repeater into another repeater.A parent repeater is used to display the main headings and child repeater populates the data according to headings. An xml file is used as datasource to display data.
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>
The above code is used to create a xml file called Group.xml. and is used as a datasource.GroupId Field is used to show the main headings And name field is used to show the child items based on the GroupId.
Aspx File
<asp:Repeater id="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_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="ChildRepeater" runat="server"> <HeaderTemplate>
<ul>
</HeaderTemplate> <FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li><%# Eval("Name_Text") %></li> </ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Codebehind File
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("./Group.xml"));
ParentRepeater.DataSource = ds;
ParentRepeater.DataBind();
}
}
protected void ParentRepeater_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");
DataRowView drv = (DataRowView)item.DataItem;
ChildRepeater.DataSource = drv.CreateChildView("category_Name");
// here u can also bind the Child Repeater with DataTable like
//ChildRepeater.DataSource = datatableName;
ChildRepeater.DataBind();
}
}
In the above code a dataset is used to read the xml file from path. MapPath() is used to find the path of xml file and repeater binds from dataset.Now onItemDataBound event of the ParentRepeater we find the main heading and binds childrepeater based on the GroupId.
Download this project to view more information.
Conclusion
This is nice to show hirarchical data using repeaters.
- Search Engine
- ASP.NET Resources
Download Source Code
Other Related and Popular Articles :
Author Profile : zafar iqbal
How would you rate the quality of this content?
Poor
Excellent
Comments
Leave New Comments
Article Content copyright by
zafar iqbal
Everything else Copyright © by
WorldofASP.NET
2009