2

我正在尝试使用站点核心导航子布局。现在我正在使用 2 个不同的。一个用于父项,它会拉出所有子项。另一个用于子项目,它只是拉它的兄弟姐妹。我想使用一种布局来拉动父母、孩子和兄弟姐妹。我现在使用的 2 没有显示 Self 或 Parent 项目。这是我在父级上使用的代码

<script runat="server">
void Page_Load(object sender, System.EventArgs e) {
DataBind();
}

IEnumerable<Sitecore.Data.Items.Item> SubItems
{
get { 
    return Sitecore.Context.Item.Children;

    }
}
</script>
<nav class="section">
<asp:Repeater ID="repSubItems" runat="server" DataSource="<%# SubItems%>">
<HeaderTemplate>
  <ul>

   </HeaderTemplate>
  <ItemTemplate>
  <li>
  <a href="<%# Sitecore.Links.LinkManager.GetItemUrl(Container.DataItem as Sitecore.Data.Items.Item) %>">
    <sc:FieldRenderer FieldName="Navigation Title" runat="server" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
    </a>
   </li>
   </ItemTemplate>
  <FooterTemplate>
  <ul>
  </FooterTemplate>
 </asp:Repeater>
  </nav>

以及拉动兄弟姐妹的子项目的导航

<nav class="section">
  <asp:Repeater runat="server" ID="navRepeater">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
    <li>
        <asp:HyperLink runat="server" id="navLink"/>
    </li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</nav>

为那个做的代码

namespace Layouts.Nav_inner_prog {


 public partial class Nav_inner_progSublayout : System.Web.UI.UserControl 
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        var parent = Sitecore.Context.Item.Parent;

        navRepeater.DataSource = parent.Children;
        navRepeater.ItemDataBound += navRepeater_ItemDataBound;
        navRepeater.DataBind();
    }

    private void navRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            var item = e.Item.DataItem as Sitecore.Data.Items.Item;
            var navLink = e.Item.FindControl("navLink") as HyperLink;

            if (item.ID == Sitecore.Context.Item.ID)
            {
                if (navLink.CssClass.Length > 0)
                {
                    navLink.CssClass += " ";
                }
                navLink.CssClass += "selected";
            }

            navLink.Text = item.DisplayName;
            navLink.NavigateUrl = Sitecore.Links.LinkManager.GetItemUrl(item);
        }
    }
  }
 }
4

1 回答 1

1

在我看来,我会改变您的方法并与 Sitecore 的最佳实践保持一致,并使用数据源来实现这一目标。

在 Sitecore 中,当您将子布局添加到演示详细信息时,将数据源添加到您要充当“父”项的项。然后使用下面的代码

<asp:Repeater runat="server" ID="NavigationRepeater" ItemType="Sitecore.Data.Items.Item" OnItemDataBound="NavigationRepeater_OnItemDataBound">
    <HeaderTemplate>
        <a href="<%# GetUrl(ParentItem) %>">
            <sc:FieldRenderer FieldName="Navigation Title" runat="server" Item="<%# ParentItem %>" />
        </a>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:HyperLink runat="server" id="navLink"/>
    </ItemTemplate>
</asp:Repeater>

然后后面的代码如下

public Sitecore.Web.UI.WebControls.Sublayout Sublayout
        {
            get
            {
                return Parent as Sitecore.Web.UI.WebControls.Sublayout;
            }
        }

        private Item _parentItem;
        public Item ParentItem
        {
            get
            {
                if (_parentItem == null)
                {
                    if (!string.IsNullOrEmpty(Sublayout.DataSource))
                    {
                        Item dbItem = Sitecore.Context.Database.GetItem(Sublayout.DataSource);

                        if (dbItem != null)
                        {
                            _parentItem = dbItem;
                        }
                    }
                }

                return _parentItem;
            }
        }

        public IEnumerable<Sitecore.Data.Items.Item> SubItems
        {
            get
            {
                return ParentItem != null ? ParentItem.Children : new List<Item>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindNavigation();
            }
        }

        public string GetUrl(Item item)
        {
            return item != null ? Sitecore.Links.LinkManager.GetItemUrl(item) : string.Empty;
        }

        private void BindNavigation()
        {
            NavigationRepeater.DataSource = SubItems;
            NavigationRepeater.DataBind();
        }

        protected void NavigationRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var item = e.Item.DataItem as Sitecore.Data.Items.Item;
                var navLink = e.Item.FindControl("navLink") as HyperLink;

                if (item.ID == Sitecore.Context.Item.ID)
                {
                    if (navLink.CssClass.Length > 0)
                    {
                        navLink.CssClass += " ";
                    }
                    navLink.CssClass += "selected";
                }

                navLink.Text = item.DisplayName;
                navLink.NavigateUrl = Sitecore.Links.LinkManager.GetItemUrl(item);
            }
        }

如果您确实必须违背最佳实践,则需要远离以区分子项和父项。这可以通过 Item 的不同 ItemTemplates 并检查 TemplateId 甚至父项上的复选框字段来完成。两者的例子如下所示;

private Item _parentItem;
public Item ParentItem
{
    get
    {
        if (_parentItem == null)
        {
            if (Sitecore.Context.Item.TemplateID == new ID("GUID OF PARENT ITEM TEMPLATE"))
            {
                _parentItem = Sitecore.Context.Item;
            }
            else
            {
                // Else set the parent of context Item to be the parent
                // Assuming you don't want a grandparent or other ancestor
                // being the parent as the wrong child will be shown
                _parentItem = Sitecore.Context.Item.Parent;
            }
        }

        return _parentItem;
    }
}
private Item _parentItem;
public Item ParentItem
{
    get
    {
        if (_parentItem == null)
        {
            if (Sitecore.Context.Item.Fields["Navigation Parent"] != null
                && Sitecore.Context.Item.Fields["Navigation Parent"].Value == "1")
            {
                _parentItem = Sitecore.Context.Item;
            }
            else if (Sitecore.Context.Item.Parent.Fields["Navigation Parent"] != null
                && Sitecore.Context.Item.Parent.Fields["Navigation Parent"].Value == "1")
            {
                _parentItem = Sitecore.Context.Item.Parent;
            }
            else
            {
                // Assuming you don't want an ancestor's child to be displayed
                _parentItem = null;
            }
        }

        return _parentItem;
    }
}
于 2015-07-23T11:57:24.523 回答