Nested Repeaters

31 Dec 2009 Matthew Lee 0 comments

The repeater is one of the most frequently used webparts in Kentico, allowing for the retrieval of structured data in a logical, ordered manner without the need for c# code, or T-SQL. It traverses the CMS tree from a given path looking for documents of a given document type, and for each that satisfies a set of given conditions, repeats a given transformation.

For example, we may want to return only the author, title, and date of publication, formatted nicely (transformation) for any blog post (document type), article (document type), or press release (document type), published before the year 2000 (condition). This can be accomplisehed without a single line of c# code, or a single line of T-SQL, using a repeater.

Every now and then though we come across a more complex example, and although a simple repeater may not satisfy the needs of this more complex example, a nested repeater may.

Nested Repeaters

A nested repeater is basically made up of a parent repeater which iterates through a set of documents, and a child repeater which executes once for each iteration of the parent repeater.

Imagine it this way, a parent repeater loops through a set of books, while a child repeater loops through the pages of each book, with the result being the content of all books.

A Worked Example

The concept is best demonstrated by introducing a worked example.

My sample bookshop project contains two document types - Book and Author, with the details of each as follows.

A book is made up of a Title, a Description, a Publisher, and a Year of Publication.

An author is made up of a First name, and a Last name.

A book may have more than one Author, and an Author is added as a child node of a Book.

In order to create the catalog page shown below, which lists all book titles and author(s), we will need to use a nested repeater.

The Parent Transformation (Book)

First of all, we will need to define the two transformations which will actually output the content.

<div>
    <strong>Category: <%# Eval(“Title”) %></strong>
</div>
<ul>
    <cms:CMSRepeater
        ID=”repeaterAuthors”
        runat=”server”
        ClassNames=”Bookstore.Author”
        TransformationName=”Bookstore.Author.authorInfo”
        Path=”./%”
        SelectOnlyPublished=”true” />
</ul>

The Child Transformation (Author)

<li><%# Eval(“Name”) %></li>

The Parent Repeater

Then we add the parent repeater, as follows.

ID=”repeaterBooks”
path=“/book/%”
transformation=”Bookstore.Book.bookInfo”

The Child Repeater

You may have noticed the weirdness in the parent transformation. What you’re actually seeing is a child repeater, defined in code.

You see, each time the parent repeater uses the parent transformation, the child repeater included in the transformation is triggered.

So, for each Book returned by the repeaterBooks repeater the bookInfo transformation is used, and each time the bookInfo transformation is used, the repeaterAuthors repeater is triggered, and for each Author returned by the repeaterAuthors repeater the authorInfo transformation is used.

The Parent Child Link

There is one very important thing that must be added for a nested repeater to work - the NestedRepeaterControlID attribute.

The NestedRepeaterControlID attribute tells the parent repeater about the child repeater, and allows the parent repeater to pass the path of each document to the child repeater.

In our case, we set the NestedRepeaterControlID to authorInfo, the ID of the child repeater.

NestedRepeaterControlID=”authorInfo” (this is the id you specific on the cmsrepeater in the second transformation)

Final Thoughts

The nested repeater concept is something that won’t necessarily make a lot of sense until you need one, at which point everything should just click.

If you run into problems with your child repeater not doing anything make sure to check your NestedRepeaterControlID as this is typically where most confusion lies.

I’ve had a few people ask me how to have something covered in Tips and Tricks, and the easiest answer is to get in touch with me via

matthew.lee@kenticodeveloper.com

If it’s someting I can cover myself I will. If not I’ll chase up the relevant people and cover it anyway.

Trackback URL: http://www.kenticodeveloper.com/trackback/a3cb5e88-646f-417f-a1c2-7e9503e9ad57/Nested-Repeaters.aspx

Comments
Blog post currently doesn't have any comments.
Leave comment Subscribe
Name:

E-mail:

Your URL:
Comments:

Enter security code:
 Security code