5,699,997 members and growing! (21,179 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

A quick guide to using nested repeaters in ASP.NET

By Nishant Sivakumar

Using nested repeaters in ASP.NET with an XML data store
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 19 Feb 2004
Updated: 19 Feb 2004
Views: 142,337
Bookmarked: 36 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 5.48 Rating: 4.02 out of 5
3 votes, 13.0%
1
2 votes, 8.7%
2
3 votes, 13.0%
3
3 votes, 13.0%
4
12 votes, 52.2%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nishant Sivakumar


Sitebuilder, Mvp
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

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.
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
GeneralBetter Way!!memberimsathy0:40 21 Jan '08  
QuestionI have a problem when use printed review to check nested repeater aspx pagesmemberYingHsuan0:02 26 Nov '07  
AnswerRe: I have a problem when use printed review to check nested repeater aspx pagesmemberYingHsuan5:47 27 Nov '07  
GeneralWith tableAdapter instead of xml filememberwerge11:14 14 Sep '07  
GeneralNested Repeatersmemberremya3003@yahoo.com18:36 22 Jun '06  
GeneralProblem in Convertingmemberkevinbhavasar2:18 3 Apr '06  
Generali want to bind to a table not xml file with teh same issue title and users under tilememberGiiiO0:28 9 Nov '05  
Generali want to bind to a table not xml file with teh same issue title and users under tilememberGiiiO0:28 9 Nov '05  
GeneralRequires at least two Child Nodesmemberjavafreddie16:55 6 Apr '05  
GeneralRe: Requires at least two Child Nodesmember7.e.Q10:06 3 Jul '07  
GeneralThis code cant be completesussAnonymous4:33 31 Mar '05  
GeneralRe: This code cant be completememberJeff Lehmer10:45 30 Sep '06  
GeneralNested User ControlsmemberMiszou15:05 14 Feb '05  
Generalneed more explanation.memberpurplerain11:20 22 Sep '04  
GeneralAn alternativememberIan Darling2:30 21 Feb '04  
GeneralRe: An alternativesussScott Galloway2:39 21 Feb '04  
GeneralRe: An alternativememberIan Darling2:42 21 Feb '04  
GeneralA couple of small points...this is not the most efficient methodmemberScott Galloway1:46 21 Feb '04  
GeneralRe: A couple of small points...this is not the most efficient methodstaffNishant S2:22 21 Feb '04  
GeneralRe: A couple of small points...this is not the most efficient methodsussScott Galloway2:34 21 Feb '04  
GeneralLots of text..memberRay Hayes0:32 20 Feb '04  
GeneralRe: Lots of text..staffNishant S0:45 20 Feb '04  
GeneralRe: Lots of text..memberRay Hayes0:51 20 Feb '04  
GeneralRe: Lots of text..staffNishant S2:27 21 Feb '04  
GeneralRe: Lots of text..memberblucas200513:58 15 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Feb 2004
Editor: Chris Maunder
Copyright 2004 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project