【问题标题】:How to get children and grandchildren in sitecore如何在sitecore中获取子孙
【发布时间】:2016-02-29 23:17:37
【问题描述】:

我正在尝试进行左侧导航,并且能够获取父项的子项,但不能获取孙子项。主文件夹下有 4 个文件夹,每个文件夹有 3 个项目,都使用相同的模板。

> Main /   
      Folder 1/
>      item1
>      item2
>      item3   
      Folder 2/
>      item1
>      item2
>      item3   
      Folder 3/
>      item1
>      item2
>      item3   
      Folder 4/
>      item1
>      item2
>      item3

我使用中继器启动:

<ul>
    <asp:Literal ID="litFolder" runat="server" />
    <asp:Repeater ID="leftNav" runat="server" OnItemDataBound="leftNav_ItemDataBound">
        <HeaderTemplate>
        <li>
        </HeaderTemplate>

        <ItemTemplate>
            <asp:Repeater ID="rptsubleftNav" runat="server" OnItemDataBound="rptsubleftNav_ItemDataBound">

                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>
                <ItemTemplate>
                        <li>
                        <asp:HyperLink ID="HLMainnav" runat="server"></asp:HyperLink>
                        </li>
                </ItemTemplate>
                <FooterTemplate>
                    </ul>
                </FooterTemplate>

            </asp:Repeater>
        </ItemTemplate>

        <FooterTemplate>
        </li>
        </FooterTemplate>
    </asp:Repeater>
</ul> 

前端设置为使它们链接,但目前它只拉入文件夹,本质上是使它们链接而不是文件夹中的项目。

 public partial class LeftNav : System.Web.UI.UserControl
 {

     Item currentItem = Sitecore.Context.Item;


     protected void Page_Load(object sender, EventArgs e)
     {
         leftNav.DataSource = Sitecore.Context.Database.GetItem("/sitecore/Main/").Children;
         leftNav.DataBind();

     }

     public void leftNav_ItemDataBound(object sender, RepeaterItemEventArgs e)
     {
         Item currentItem = (Item)e.Item.DataItem;
     ((Literal)e.Item.FindControl("litFolder")).Text = currentItem.Name;

        if (currentItem != null)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var subNav = e.Item.FindControl("rptsubleftNav") as Repeater;

                if (subNav != null)
                {
                    subNav.DataSource = currentItem.GetChildren();
                    subNav.DataBind();
                    currentItem.ToString();

                    HyperLink HLMainnav = (HyperLink)e.Item.FindControl("HLMainnav");
                    HLMainnav.NavigateUrl = LinkManager.GetItemUrl(currentItem);
                    HLMainnav.Text = currentItem.Name;
                }
            }
        }
    }

    protected void rptsubleftNav_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

如何获得下一个级别并将它们设置为链接而不是文件夹?

(已更新) 我有文件夹及其子文件夹,但未显示名称。我还尝试在前端使用 ,但它表示 Sitecore 项目没有该字段。所以不确定它如何正确显示项目但没有标题。

【问题讨论】:

    标签: c# sitecore sitecore8


    【解决方案1】:

    您需要在leftNav 中继器ItemTemplate 标记内添加另一个中继器。

    然后在leftNav_ItemDataBound 方法中,获取currentItem 的子级并将它们分配为新中继器的数据源。

    这里有类似的问题(Sitecore) Navigation with Subnavigation

    就是这样。

    【讨论】:

    • 不要忘记从生成链接中排除文件夹项目。您可以使用 item.TemplateID == [FolderTemplateID] 之类的东西将它们从列表中排除或不为它们生成链接。
    • 这可行,但无论出于何种原因,它没有显示文件夹或项目的名称,我将 currentItem 设置为字符串,但它仍然没有显示
    • 您需要编辑您的问题并在现有代码下方发布更新的代码
    • 你为什么在if (subNav != null) ... else 中有else部分?这意味着它只会在没有 subNav 的情况下执行此代码,并且 subNav 始终存在,对吗? (顺便说一句,我在你的 ascx 代码中根本看不到 subNav - 我想你忘了更新这个)
    • 当我在 if 语句中添加它时,它说 HLMainnav.NavigateUrl = LinkManager.GetItemUrl(currentItem);没有设置为对象的实例,而它显然是在之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2015-09-06
    • 2012-03-02
    • 1970-01-01
    相关资源
    最近更新 更多