How to bind nested repeaters with a selected data source

   —   
You may need to select data from two tables manually using API and bind this data source to nested repeaters. This article provides an API example which helps you to achieve the goal.
Our goal is to generate output from cms.blogmonth and  cms.blog post document types. For example, to generate the structure:
 
March 2011
   - Expanding to Europe
   - Remote Management
April 2012
   - My first blog post
 
To create a data source, you can call our API:
 
// Select blog month and blog post documents
DataSet documents = TreeHelper.GetDocuments(CMSContext.CurrentSiteName, "/%", "en-US", true, "cms.blogmonth;CMS.BlogPost", null, null, -1, false, 0);


This kind of dataset contains two tables; one with months and one with posts. You need to create a relation between these two tables and bind a data source to a parent repeater. Relations in Kentico between parent and child are created in columns NodeID and NodeParentID:

protected void Page_Load(object sender, EventArgs e)
{
        DataSet documents = TreeHelper.GetDocuments(CMSContext.CurrentSiteName, "/%", "en-US", true, "cms.blogmonth;CMS.BlogPost", null, null, -1, false, 0);

        if (!DataHelper.DataSourceIsEmpty(documents))
        {
            //Create the relation between the Authors and Titles tables.
            documents.Relations.Add("myrelation",
            documents.Tables[0].Columns["NodeID"],
            documents.Tables[1].Columns["NodeParentID"]);

            parentRepeater.DataSource = documents.Tables[0];
            Page.DataBind();
        }
}


To display data, now you need to add the following controls to the markup:

<%@ Import Namespace="System.Data" %> <asp:Repeater id="parentRepeater" runat="server">
<itemtemplate>      
<b><%# DataBinder.Eval(Container.DataItem, "BlogMonthName")%></b>         
<asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' >   
<itemtemplate>    
<input type="checkbox" name="ProgramId" value="<%# DataBinder.Eval(Container.DataItem, "[\"BlogPostID\"]")%>"/>                
<%# DataBinder.Eval(Container.DataItem, "[\"BlogPostTitle\"]")%>
</itemtemplate>     
</asp:repeater>      
</itemtemplate>
</asp:Repeater>
 
-it-


See also:  Hierarchical transformation

Applies to: Kentico CMS 6.x
Share this article on   LinkedIn