Sitecore 使用 Nested Repeater 正确显示父项和子项

Sitecore Show parent and child items correctly using Nested Repeater

在 sitecore 中我有以下结构:

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

我正在使用 Nested repeater 在 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>

下面是代码:

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();
}

我得到的结果是这样的:

  • 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

不知道怎么获取currentItem然后绑定数据

您的内部转发器始终以 _countryNameItems 为数据源。用这个替换 ItemDataBound 方法应该会有所帮助:

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();
}

我对 sitecore 没有那么多经验,但你基本上在这里做错了。

您正在为每个中继器设置相同的 List<Item>,即 _countryNameItems。因此,理想情况下,您需要在 ItemDatabound 内拉出不同的 List<Item>。你可以试试这样的

标记(将上面的行更改为这样的innerRepeater)

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

代码隐藏

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();
}