Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET

Nested Collapsible Repeater

Rate me:
Please Sign up or sign in to vote.
4.40/5 (7 votes)
16 Mar 2009CPOL1 min read 35.2K   498   19   2
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
<?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:

ASP.NET
<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.NET
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
   DataFile="~/XMLFile.xml"> </asp:XmlDataSource> 

Image 1

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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.