Sitecore导航菜单子布局

时间:2015-07-23 11:25:25

标签: navigation sitecore ascx sublayout

我正在尝试使用sitecore导航子布局。现在我正在使用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);
        }
    }
  }
 }

1 个答案:

答案 0 :(得分:1)

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

在Sitecore中,当您将Sublayout添加到Presentation Details时,将Datasource添加到您想要充当“Parent”项的Item。然后使用以下代码

-(NSString *)add:(Fraction *)addF {

    CGFloat num = self.numerator * addF.denominator + self.denominator * addF.numerator; 
    CGFloat den = self.denominator * addF.denominator; 
    NSString * fraction = [NSString stringWithFormat:@"%f/%f",num,den];
    return fraction; 
}

然后背后的代码如下

<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>

如果您不得不违反最佳做法,则需要离开以区分SubItem和Parent Item。这可以通过Item的不同ItemTemplates来完成,并检查TemplateId甚至是Parent Item上的复选框字段。两者的例子分别如下所示;

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