Click here to Skip to main content
Click here to Skip to main content

Nested Collapsible Repeater

, 16 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A nested repeater control.

Introduction

The Repeater control available with the .NET Framework does a pretty good job of templated data-binding. But, it does not support data binding with hierarchical data sources. In ASP.NET, data binding will essentially bind data to a server-side control that will then render that data in some form to the client. The server-side control must support a property called DataSource and a method called DataBind(), and the data source to which the control is bound implements the IEnumerable (IHierarchicalEnumerable for nested data) interface.

I decided to write a nested Repeater from ground up. The control renders hierarchical data like a treeview, and supports expanding and collapsing nodes. Each node has a header, body, and footer component, each of which can be templated. The control uses a mix of Composition and Rendering to do the job. Using Composition is generally very expensive as server side HTML elements are created for every tag. So, I am using Composition for rendering the header, body, and footer, and Rendering for all the other tags. For client-side behavior, I have implemented an IScriptControl which essentially manages expanding and collapsing the nodes.

Any data source that derives from HierarchicalDataSourceControl can be used to bind to this control; e.g., XMLDataSource, SiteMapDataSource.

Using the code

<?xml version="1.0" encoding="utf-8" ?>
<Item Header="Header" Footer ="Footer">
  Some Text
  <Item Header="Header" Footer ="Footer">
    ...
</Item>

The repeater can be declared in the ASPX file as:

<cc1:RepeaterEx ID="RepeaterEx1" runat="server">
    <HeaderTemplate>
        <div style="background-color: Red">
            <%#XPath("@Header")%>
        </div>
    </HeaderTemplate>
    <BodyTemplate>
        <div style="background-color: Yellow">
            <%#XPath("text()")%>
        </div>
    </BodyTemplate>
    <FooterTemplate>
        <div style="background-color: Green">
            <%#XPath("@Footer")%>
        </div>
    </FooterTemplate>
</cc1:RepeaterEx>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
   DataFile="~/XMLFile.xml"> </asp:XmlDataSource> 

Please feel free to post your comments and suggestions.

References

License

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

Share

About the Author

Alomgir Miah A

United States United States
No Biography provided

Comments and Discussions

 
QuestionUseful, but is it possible to add asp.net controls in the xml? Pinmemberkintz17-Jun-10 8:25 
QuestionGood article! Pinmemberpandian.anna16-Mar-09 7:32 

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
Web04 | 2.8.141223.1 | Last Updated 16 Mar 2009
Article Copyright 2009 by Alomgir Miah A
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid