Click here to Skip to main content
13,050,429 members (132,374 online)
Click here to Skip to main content
Add your own
alternative version


69 bookmarked
Posted 19 Feb 2004

A quick guide to using nested repeaters in ASP.NET

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


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.


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();
    CategoryRepeater.DataSource = ds;

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");

That's all.

The output

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


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.


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


About the Author

Nish Nishant
United States United States
Nish Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

You may also be interested in...


Comments and Discussions

GeneralAn alternative Pin
Ian Darling21-Feb-04 1:30
memberIan Darling21-Feb-04 1:30 
GeneralRe: An alternative Pin
Scott Galloway21-Feb-04 1:39
sussScott Galloway21-Feb-04 1:39 
GeneralRe: An alternative Pin
Ian Darling21-Feb-04 1:42
memberIan Darling21-Feb-04 1:42 
GeneralA couple of small points...this is not the most efficient method Pin
Scott Galloway21-Feb-04 0:46
memberScott Galloway21-Feb-04 0:46 
GeneralRe: A couple of small points...this is not the most efficient method Pin
Nishant S21-Feb-04 1:22
staffNishant S21-Feb-04 1:22 
GeneralRe: A couple of small points...this is not the most efficient method Pin
Scott Galloway21-Feb-04 1:34
sussScott Galloway21-Feb-04 1:34 
GeneralLots of text.. Pin
Ray Hayes19-Feb-04 23:32
memberRay Hayes19-Feb-04 23:32 
GeneralRe: Lots of text.. Pin
Nishant S19-Feb-04 23:45
staffNishant S19-Feb-04 23:45 
Ray Hayes wrote:
I'd simply solve the problem you presented with a short XSL page.

Hello Ray

I guess, by using the XML file I might have inadvertently misplaced the focus of the article from the nested repeaters to the XML data. I'll try and make things clearer using a more-real-life example.

Lets take CP as an example case. CP has a top level category classification – C++, ASP.NET, .NET, All topics etc. Lets assume that these categories are stored in an Excel file. Under each category we have sections where some sections might belong to multiple categories. Now assume that these sections are stored in a SQL server database (each section has a field for the top-level category. Now assume that for each section we have an XML file (or a CSV file) that lists all articles under that section.

Now using nested repeaters (3 levels required) we can very easily generate a page that lists all categories/sections/articles.

The outermost repeater binds to the excel sheet’s specific worksheet/column, the middle repeater binds to the SQL server table, and the innermost repeater binds to the XML file.

The outer repeater is setup to generate a combobox that lists all categories, whose onchange event is handled to generate a page using the middle repeater. The middle repeater is setup to generate tables – one table for each section. Thus depending on the category chosen in the combobox we list those sections that are associated with that top-level category. Now the inner most repeater is used to list individual articles corresponding to each section.

I hope I have made the example clear.

So its not just about XML. The repeater control is one real cool control, and nested repeaters make it even better.


Extending MFC Applications with the .NET Framework [NW]
Summer Love and Some more Cricket [NW] (My first novel)
Shog's review of SLASMC [NW]
Nish is now the first and only CPian (as of now) to reach 16,000 forum posts on CodeProject.
GeneralRe: Lots of text.. Pin
Ray Hayes19-Feb-04 23:51
memberRay Hayes19-Feb-04 23:51 
GeneralRe: Lots of text.. Pin
Nishant S21-Feb-04 1:27
staffNishant S21-Feb-04 1:27 
GeneralRe: Lots of text.. Pin
blucas200515-Mar-07 12:58
memberblucas200515-Mar-07 12:58 
GeneralOT: New image Pin
Uwe Keim19-Feb-04 21:41
sitebuilderUwe Keim19-Feb-04 21:41 
GeneralRe: OT: New image Pin
Nishant S19-Feb-04 23:32
staffNishant S19-Feb-04 23:32 
GeneralRe: OT: New image Pin
Uwe Keim19-Feb-04 23:34
sitebuilderUwe Keim19-Feb-04 23:34 
GeneralRe: OT: New image Pin
Jonny Newman20-Feb-04 2:38
memberJonny Newman20-Feb-04 2:38 
GeneralRe: OT: New image Pin
Shog921-Feb-04 15:31
memberShog921-Feb-04 15:31 
GeneralRe: OT: New image Pin
Jonny Newman22-Feb-04 1:33
memberJonny Newman22-Feb-04 1:33 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Feb 2004
Article Copyright 2004 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid