Click here to Skip to main content
Click here to Skip to main content

A quick guide to using nested repeaters in ASP.NET

, 19 Feb 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Using nested repeaters in ASP.NET with an XML data store

Introduction

I've never really been much of a web-developer and never thought I'd find web-development all that interesting. But I must say I've been quite fascinated by what little ASP.NET I've done up till now, which is not a lot to be honest. One control I found particularly useful was the Repeater control, but I struggled a little when I tried to implement nested repeaters using an XML file as the data store. Eventually, the solution turned out to be embarrassingly easy, and I thought I'd write a little article for other first-timers who might encounter the same annoying situation I did.

Note to readers

I assume that you already know how to use a Repeater control. This article only shows you how to implement nested repeaters and will not attempt to explain repeaters in general.

Example

I am going to demonstrate a simple ASP.NET web application that will list out a Cricket World XI using an XML file as the input-data. Eventually, modification of the team simply involves a change in the XML file with no changes required either in the aspx pages or in the code-behind files.

My XML file

Essentially I have four categories - and each category has one or more players.

Implementing nested repeaters

I am going to list the categories first and inside each category I will list the players under that category. Lets first add the outter repeater that will list the categories.

We now add the inner repeater to the <ItemTemplate> tag of the outter repeater.

Writing the code-behind code

Alright, I know that "code-behind code" sounds weird, but I couldn't think of anything better sounding and if anyone has any better ideas, please drop me a line. Anyway we setup the first repeater in the Page_Load event handler as usual.

private void Page_Load(object sender, System.EventArgs e)
{
    DataSet ds = new DataSet();
    ds.ReadXml(MapPath("./XMLFile1.xml"));
    CategoryRepeater.DataSource = ds;
    CategoryRepeater.DataBind();
}

For setting up the outter repeater, we handle the ItemDataBound event of the Repeater class which is raised when an item is data-bound but before it is rendered on the page. We now get a reference to the PlayerRepeater control using RepeaterItem.FindControl and set its data source using CreateChildView and the automatic relation that's made for us - category_cricketer. By the way I was quite impressed by that, I never expected automatic relations to be created based on the XML. Pretty cool I think!

private void CategoryRepeater_ItemDataBound(object sender, 
    System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    if( (item.ItemType == ListItemType.Item) ||
        (item.ItemType == ListItemType.AlternatingItem) )
    {
        PlayerRepeater = (Repeater) item.FindControl("PlayerRepeater");
        DataRowView drv = (DataRowView)item.DataItem;
        PlayerRepeater.DataSource = drv.CreateChildView("category_cricketer");
        PlayerRepeater.DataBind();
    }
}

That's all.

The output

I got the below output when I viewed the web-form in my browser.

Conclusion

Feedback and criticism is welcome as usual. I'd also like to thank Aravind Corera (Chennai based C# MVP) who gave me the right URLs to solve this problem when I was tearing my hair out in frustration.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

 
QuestionGood Article PinmemberAlireza_136219-Mar-13 22:45 
QuestionPossible to include paging? PinmemberTyng20-Jan-13 14:24 
QuestionA better Tutorial here Pinmembertim cadieux26-Jul-12 6:50 
GeneralBetter Way!! Pinmemberimsathy20-Jan-08 23:40 
GeneralRe: Better Way!! PinmemberMaciej Pilichowski5-Mar-11 23:31 
GeneralRe: Better Way!! PinmemberMarco Bertschi4-Mar-13 7:36 
QuestionI have a problem when use printed review to check nested repeater aspx pages PinmemberYingHsuan25-Nov-07 23:02 
AnswerRe: I have a problem when use printed review to check nested repeater aspx pages PinmemberYingHsuan27-Nov-07 4:47 
GeneralWith tableAdapter instead of xml file Pinmemberwerge14-Sep-07 10:14 
GeneralNested Repeaters Pinmemberremya3003@yahoo.com22-Jun-06 17:36 
GeneralProblem in Converting Pinmemberkevinbhavasar3-Apr-06 1:18 
Generali want to bind to a table not xml file with teh same issue title and users under tile PinmemberGiiiO8-Nov-05 23:28 
Generali want to bind to a table not xml file with teh same issue title and users under tile PinmemberGiiiO8-Nov-05 23:28 
how can i do it using a datable
 
gIo
GeneralRequires at least two Child Nodes Pinmemberjavafreddie6-Apr-05 15:55 
GeneralRe: Requires at least two Child Nodes Pinmember7.e.Q3-Jul-07 9:06 
GeneralThis code cant be complete PinsussAnonymous31-Mar-05 3:33 
GeneralRe: This code cant be complete PinmemberJeff Lehmer30-Sep-06 9:45 
GeneralRe: This code cant be complete PinmemberAlex Stephens7-Jan-13 2:32 
GeneralNested User Controls PinmemberMiszou14-Feb-05 14:05 
Generalneed more explanation. Pinmemberpurplerain22-Sep-04 10:20 
GeneralAn alternative PinmemberIan Darling21-Feb-04 1:30 
GeneralRe: An alternative PinsussScott Galloway21-Feb-04 1:39 
GeneralRe: An alternative PinmemberIan Darling21-Feb-04 1:42 
GeneralA couple of small points...this is not the most efficient method PinmemberScott Galloway21-Feb-04 0:46 
GeneralRe: A couple of small points...this is not the most efficient method PinstaffNishant S21-Feb-04 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 20 Feb 2004
Article Copyright 2004 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid