Click here to Skip to main content
Licence CPOL
First Posted 16 Mar 2009
Views 10,230
Downloads 115
Bookmarked 17 times

Nested Collapsible Repeater

By Alomgir Miah A | 16 Mar 2009
A nested repeater control.

1

2
2 votes, 28.6%
3
1 vote, 14.3%
4
4 votes, 57.1%
5
4.40/5 - 7 votes
μ 4.40, σa 1.66 [?]

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)

About the Author

Alomgir Miah A



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUseful, but is it possible to add asp.net controls in the xml? Pinmemberkintz8:25 17 Jun '10  
QuestionGood article! Pinmemberpandian.anna7:32 16 Mar '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 16 Mar 2009
Article Copyright 2009 by Alomgir Miah A
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid