Click here to Skip to main content
11,412,833 members (73,474 online)
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 Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

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 CodeProject.com 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 : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
QuestionNested Repeater with parametr Pin
Member 11572306 at 1-Apr-15 1:17
memberMember 115723061-Apr-15 1:17 
AnswerRe: Nested Repeater with parametr Pin
Btihin at 8-Apr-15 1:37
memberBtihin8-Apr-15 1:37 
QuestionGood Article Pin
Alireza_1362 at 19-Mar-13 23:45
memberAlireza_136219-Mar-13 23:45 
QuestionPossible to include paging? Pin
Tyng at 20-Jan-13 15:24
memberTyng20-Jan-13 15:24 
QuestionA better Tutorial here Pin
tim cadieux at 26-Jul-12 7:50
membertim cadieux26-Jul-12 7:50 
GeneralBetter Way!! Pin
imsathy at 21-Jan-08 0:40
memberimsathy21-Jan-08 0:40 
GeneralRe: Better Way!! Pin
Maciej Pilichowski at 6-Mar-11 0:31
memberMaciej Pilichowski6-Mar-11 0:31 
GeneralRe: Better Way!! Pin
Marco Bertschi at 4-Mar-13 8:36
memberMarco Bertschi4-Mar-13 8:36 
QuestionI have a problem when use printed review to check nested repeater aspx pages Pin
YingHsuan at 26-Nov-07 0:02
memberYingHsuan26-Nov-07 0:02 
AnswerRe: I have a problem when use printed review to check nested repeater aspx pages Pin
YingHsuan at 27-Nov-07 5:47
memberYingHsuan27-Nov-07 5:47 
GeneralWith tableAdapter instead of xml file Pin
werge at 14-Sep-07 11:14
memberwerge14-Sep-07 11:14 
GeneralNested Repeaters Pin
remya3003@yahoo.com at 22-Jun-06 18:36
memberremya3003@yahoo.com22-Jun-06 18:36 
GeneralProblem in Converting Pin
kevinbhavasar at 3-Apr-06 2:18
memberkevinbhavasar3-Apr-06 2:18 
Generali want to bind to a table not xml file with teh same issue title and users under tile Pin
GiiiO at 9-Nov-05 0:28
memberGiiiO9-Nov-05 0:28 
Generali want to bind to a table not xml file with teh same issue title and users under tile Pin
GiiiO at 9-Nov-05 0:28
memberGiiiO9-Nov-05 0:28 
GeneralRequires at least two Child Nodes Pin
javafreddie at 6-Apr-05 16:55
memberjavafreddie6-Apr-05 16:55 
GeneralRe: Requires at least two Child Nodes Pin
7.e.Q at 3-Jul-07 10:06
member7.e.Q3-Jul-07 10:06 
GeneralThis code cant be complete Pin
Anonymous at 31-Mar-05 4:33
sussAnonymous31-Mar-05 4:33 
GeneralRe: This code cant be complete Pin
Jeff Lehmer at 30-Sep-06 10:45
memberJeff Lehmer30-Sep-06 10:45 
GeneralRe: This code cant be complete Pin
Alex Stephens at 7-Jan-13 3:32
memberAlex Stephens7-Jan-13 3:32 
GeneralNested User Controls Pin
<a href='http://www.miszou.com/'>Miszou</a> at 14-Feb-05 15:05
memberMiszou14-Feb-05 15:05 
Generalneed more explanation. Pin
purplerain at 22-Sep-04 11:20
memberpurplerain22-Sep-04 11:20 
GeneralAn alternative Pin
Ian Darling at 21-Feb-04 2:30
memberIan Darling21-Feb-04 2:30 
GeneralRe: An alternative Pin
Scott Galloway at 21-Feb-04 2:39
sussScott Galloway21-Feb-04 2:39 
GeneralRe: An alternative Pin
Ian Darling at 21-Feb-04 2:42
memberIan Darling21-Feb-04 2:42 
GeneralA couple of small points...this is not the most efficient method Pin
Scott Galloway at 21-Feb-04 1:46
memberScott Galloway21-Feb-04 1:46 
GeneralRe: A couple of small points...this is not the most efficient method Pin
Nishant S at 21-Feb-04 2:22
staffNishant S21-Feb-04 2:22 
GeneralRe: A couple of small points...this is not the most efficient method Pin
Scott Galloway at 21-Feb-04 2:34
sussScott Galloway21-Feb-04 2:34 
GeneralLots of text.. Pin
Ray Hayes at 20-Feb-04 0:32
memberRay Hayes20-Feb-04 0:32 
GeneralRe: Lots of text.. Pin
Nishant S at 20-Feb-04 0:45
staffNishant S20-Feb-04 0:45 
GeneralRe: Lots of text.. Pin
Ray Hayes at 20-Feb-04 0:51
memberRay Hayes20-Feb-04 0:51 
GeneralRe: Lots of text.. Pin
Nishant S at 21-Feb-04 2:27
staffNishant S21-Feb-04 2:27 
GeneralRe: Lots of text.. Pin
blucas2005 at 15-Mar-07 13:58
memberblucas200515-Mar-07 13:58 
GeneralOT: New image Pin
Uwe Keim at 19-Feb-04 22:41
sitebuilderUwe Keim19-Feb-04 22:41 
GeneralRe: OT: New image Pin
Nishant S at 20-Feb-04 0:32
staffNishant S20-Feb-04 0:32 
GeneralRe: OT: New image Pin
Uwe Keim at 20-Feb-04 0:34
sitebuilderUwe Keim20-Feb-04 0:34 
GeneralRe: OT: New image Pin
Jonny Newman at 20-Feb-04 3:38
memberJonny Newman20-Feb-04 3:38 
GeneralRe: OT: New image Pin
Shog9 at 21-Feb-04 16:31
memberShog921-Feb-04 16:31 
GeneralRe: OT: New image Pin
Jonny Newman at 22-Feb-04 2:33
memberJonny Newman22-Feb-04 2:33 

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 | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 20 Feb 2004
Article Copyright 2004 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid