Nested Repeater controls using .NET

Nested Repeater control. How to call a Repeater inside a Repeater control using .NET. Code to create a nested repeater using .NET. Advantages of WebControl Class in .NET. Free code to create a Nested Repeater using .NET. Explanation of Nested Repeater in .NET with examples.


Hope you've all heard about the DataGrid, DataList and the Repeater Controls.
Come on, I'm sure you've at least seen them used across the web…
DotNetSpider has used it in almost every page.
Check out The Questions section, The Articles section…. Noticed how the data has been displayed???

Yes, DataGrid, DataList and the Repeater Controls are the three main data display controls that ships with ASP.NET.
Well, knowing ‘which to use when' is what's important!

Why would Microsoft create three display controls?
Naturally, each of these controls is suited for different tasks and has their own plus and minus points.

Here are a few guidelines that will help you pick the appropriate control depending on your requirements.


Amongst all, I'd say this one is the easiest to develop.
It provides us with features like: Sorting, Paging & Editing.
Its derived from the WebControl class,
and hence we can set its BorderStyle , BackColor etc.
But coming to the performance factor, The DataGrid Control is stated to have the worst performance of the three data web controls.
And another drawback is that, we can't customize it much according to our needs.
The DataGrid sticks to its simple tabular layout, and that's where the other two Controls win the poll.


The DataList can be used to provide a more customizable interface to the user.
Again this one too is derived from the WebControl class, and hence we can set the same properties.
Using the RepeatColumns property, you can specify how many DataList items should appear per table row.
DataList does provide inline editing, but if you consider the development time required to add in such functionalities,
I'd say be wise in your decision coz this one's a bit time consuming!


This control does the least for us, It just does what it says - “Repeats"!
It supports neither paging nor editing.
It's mainly used to show hierarchical data, like forums.
The control is real handy when it comes to providing a customizable interface to the user. The Repeater class is not derived from the WebControl class, and hence it lacks those stylistic properties.
Coming to the performance issue, this one rules over the other two controls.

Nested Repeater Controls

In this article I'm going to deal with the Repeater Control, to be specific – “Nested Repeater Controls".
By Nested repeaters, I mean embedding one repeater control within another.

If you are looking for the source code to bind your data source to a single repeater control,
or for the basics of the Repeater control, then just type in your requirement in the search engine you find on top, and click Search.
Welcome back to this page when you are ready to learn about nesting repeater controls :-)

Where would you use nested repeaters???
Well, Let's suppose we have a requirement to display all the States, and the list of the Schools within that particular State.

What would we do to get our output look like this

Carmel School
St.Mary's School
Crescent public school
Donbosco School
St.Josephs School
Sacred Heart School

As we all know, the number of Schools can vary from state to state.
See, now that's a situation where a nested repeater comes handy.

Let's begin coding…
Paste the following code within the <form runat="server"> </form> tags of your .aspx file.

<TABLE id="Table1" border="0">
<asp:repeater id="myRepeater" runat="server">
<TD><b><u><%#DataBinder.Eval(Container.DataItem, "State")%></u></b></TD>
<asp:repeater id="NestedRepeater" runat="server">

Next, Move to your Page_Load( ) event and bind your main repeater.

private void Page_Load(object sender, System.EventArgs e)
SqlConnection con= new SqlConnection("Enter your connection string here");
SqlDataAdapter sdap =
new SqlDataAdapter("select distinct(State) from listOFschools;select * from listOFschools",con);
DataSet ds = new DataSet();
new DataRelation("NestThem",ds.Tables[0].Columns["State"], ds.Tables[1].Columns["State"])
myRepeater.DataSource = ds;

Finally, Enter the following Code in your Main Repeaters ItemDataBound( ) event to bind your nested repeater.

private void myRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
DataRowView dv = e.Item.DataItem as DataRowView;
if(dv != null)
Repeater nestedRepeater = e.Item.FindControl("NestedRepeater") as Repeater;
if(nestedRepeater != null)
nestedRepeater.DataSource = dv.CreateChildView("NestThem");

The codes are self-explanatory;
we're just embedding a repeater control within another.
As you can see, it's the Relations property of the dataset that draws out the required nested fields.

Run your program and that's it! We now have a nested repeater.
So, Use the Nested Repeater control at the appropriate situation and enhance the look of your page.


Now that you've learned something about the Repeater control, try doing this simple Online Test:
  • Drop in your feedbacks about this article using the Feedbacks link below and get back to this page.
    You'll see how skillfully your message has been displayed using one of the data display controls :-D

  • Comments

    Author: Sadha Sivam04 Jan 2005 Member Level: Gold   Points : 0

    Really a good one....

    I am came to the dot net spider article section after a long time and saw some couple of good and informative.

    Really this one is good... Good work anitha...

    With thanks,

    Sadha Sivam S,

    Author: Stephen Viswaraj10 Feb 2005 Member Level: Bronze   Points : 0

    Very good article anita. actually, I've prepared the document which describes the difference among these three controls. some points are new from my preparation.

    Cool.. Keep posting on it...

    Author: Tim Farrell15 Mar 2006 Member Level: Bronze   Points : 0


    My problem is handling this with multiple tables. Your example only deals with one table.

    In my application I have 3 or more tables that I need to nest.

    Can you recommend a resource or solution for this scenario.

    Thank you.


    Author: Preet03 Dec 2008 Member Level: Gold   Points : 1

    Really gr8 article,
    Easy to understand, clearly defined, containing everything that a user would require related to these data controls.
    Keep on Posting these type of articles.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: