Kamran
Kamran

Reputation: 4100

Sitecore Show parent and child items correctly using Nested Repeater

In the sitecore I have following structure:

  • Europe
    • Germany
    • France
    • Sweden
  • Asia
    • China
    • Thailand
  • Middle East
    • U.A.E
    • Iran

I am using Nested repeater to show this structure in Asp.net

<asp:Repeater runat="server" ID="repSubsidiaryList">
<ItemTemplate>
<li>
 <%# Helper.Field(Container.DataItem as Item, "Short Title)%> 
   <asp:Repeater ID="InnerRepeater" runat="server">
     <ItemTemplate>
       <li>
        <%# Helper.Field(Container.DataItem as Item, "Name")%>
       </li>
      </ItemTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
</asp:Repeater>

Here is Code behind:

List<Item> _countryNameItems = new List<Item>(); 
protected void Page_Load(object sender, EventArgs e)
{
    //get the item Subsidaries in sitecore 
    Item mainSubsidiaryFolderItem = Sitecore.Context.Database.GetItem(Settings.GetSetting("SubsidariesFolder"));

    _countryNameItems = mainSubsidiaryFolderItem.Axes.GetDescendants().Where(p => p.TemplateName == "Subsidiary").ToList();

    // binding Subsidiary Folder to a repeater
    repSubsidiaryList.DataSource = mainSubsidiaryFolderItem.GetChildren();
    repSubsidiaryList.DataBind();
}

private void repSubsidiaryList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    InnerRepeater = (Repeater) item.FindControl("InnerRepeater");
    InnerRepeater.DataSource = _countryNameItems;
    InnerRepeater.DataBind();
}

I am getting the results like this:

  • Europe
    • Germany
    • France
    • Sweden
    • China
    • Thailand
    • U.A.E
    • Iran
  • Asia
    • Germany
    • France
    • Sweden
    • China
    • Thailand
    • U.A.E
    • Iran
  • Middle East
    • Germany
    • France
    • Sweden
    • China
    • Thailand
    • U.A.E
    • Iran

I don't know how to get the currentItem and then bind the data.

Upvotes: 2

Views: 1295

Answers (2)

Martin Davies
Martin Davies

Reputation: 4456

Your inner repeater is always datasourced to _countryNameItems. Replacing the ItemDataBound method with this should help:

private void repSubsidiaryList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
   // Cast the current dataitem to a Sitecore item
   Item item = (Item)e.Item.DataItem; 
   Repeater innerRpt = (Repeater) e.Item.FindControl("InnerRepeater");      
   // bind the inner repeater to the children of the sitecore item
   innerRpt.DataSource = item.Children; 
   innerRpt.DataBind();
}

Upvotes: 3

codeandcloud
codeandcloud

Reputation: 55200

I am not that experienced with sitecore, but you are basically doing things wrong here.

You are setting the same List<Item>, i.e, _countryNameItems to each of your repeater. So you ideally need to pull different List<Item> inside the ItemDatabound. You could try something like this

Markup ( change the line above like innerRepeater like this )

<asp:Label ID="parentGroupLabel" runat="server" 
        Text='<%# Helper.Field(Container.DataItem as Item, "Short Title")%>'>
</asp:Label>

Code-behind

protected void repSubsidiaryList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    var templateName = (Label) item.FindControl("parentGroupLabel");
    var InnerRepeater = (Repeater) item.FindControl("InnerRepeater");
    _countryNameItems = mainSubsidiaryFolderItem.Axes.GetDescendants().Where(p => p.TemplateName == templateName.Text).ToList();
    InnerRepeater.DataSource = _countryNameItems;
    InnerRepeater.DataBind();
}

Upvotes: 1

Related Questions