如果需要,从Repeater内部的codebehind引用函数以获取不同的项URL

时间:2017-03-24 17:53:55

标签: c# asp.net linq sitecore sitecore7.2

我正在努力更新网站上的导航代码,以便检查模板中是否选择了某个字段。导航使用中继器构建,并具有linq样式编码功能。导航深度为3级,任何页面都可以选中一个标记为“重定向到第一个子项”的复选框字段。如果选中此字段,那么我想运行此功能:

private string GetURL(ChaNavItem item)
{
    string returnedURL = string.Empty;

    // Check to see whether or nor a redirect to the first child is needed
    if (item.RedirectToFirstChild == true)
    {
        Item urlItem = db.Items[item.SitecoreID.ToString()];
        if (urlItem.HasChildren)
        {
            returnedURL = Sitecore.Links.LinkManager.GetItemUrl(urlItem.Children[0]);
        }
        else
        {
            returnedURL = item.URL;
        }
    }
    else
    {
        returnedURL = item.URL;
    }

    return returnedURL;
}

该函数获取Sitecore项并检查要返回的URL。我希望也许我可以在转发器中以某种方式使用此功能,我设置链接。目前,我只是获取该项目的当前链接,而不是其他任何内容:

<a href="<%# Sitecore.Links.LinkManager.GetItemUrl(Item) %>" runat="server" class="child"><%# Item.Fields["Menu Title"].Value %>

看起来它应该很简单,但它不是(至少对我而言)。以下是设置转发器数据的参考代码:

private void Page_Load(object sender, EventArgs e)
{
    currentItem = Sitecore.Context.Item;
    CurrentMenuTitle.Text = currentItem.Fields["Menu Title"].Value;
    var db = Sitecore.Context.Database;
    homePageItem = db.Items[homePageId];


    var TopLevelItems = currentItem.Children.Where(c => c.Fields["Hide From Menus"].Value != "1" && c.Fields["Hidden Page"].Value != "1");
    if (currentItem.TemplateID.ToString() != topLevelNavTemplateID)
    {
        var ancestors = currentItem.Axes.GetAncestors().ToList<Item>();
        foreach (Item ancestor in ancestors)
        {
            if (ancestor.TemplateID.ToString() == topLevelNavTemplateID)
            {
                TopLevelItems = ancestor.Children.Where(c => c.Fields["Hide From Menus"].Value != "1" && c.Fields["Hidden Page"].Value != "1");
            }
        }
    }
    SideNavRepeater.DataSource = TopLevelItems;
    SideNavRepeater.DataBind();

}

protected void SideNav_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var secondTierItem = e.Item.DataItem as Item;
    Repeater SiteNavSndLevelRepeater = e.Item.FindControl("SiteNavSndLevelRepeater") as Repeater;
    var thirdTierNavItems = secondTierItem.Children
       .Where(c => c.Fields["Hide From Menus"].Value != "1" && c.Fields["Hidden Page"].Value != "1");

    var arrowContainer = e.Item.FindControl("RightArrowContainer") as Panel;
    var indentationContainer = e.Item.FindControl("IndentedItemContainer") as Panel;

    if (thirdTierNavItems.Count() == 0)
    {
        arrowContainer.Visible = false;
        indentationContainer.Visible = true;
    }

    SiteNavSndLevelRepeater.DataSource = thirdTierNavItems;
    SiteNavSndLevelRepeater.DataBind();

}

protected void SiteNavSndLevel_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    var secondTierItem = e.Item.DataItem as Item;
    Repeater SiteNavThirdLevelRepeater = e.Item.FindControl("SiteNavThirdLevelRepeater") as Repeater;
    var fourthTierNavItems = secondTierItem.Children
       .Where(c => c.Fields["Hide From Menus"].Value != "1" && c.Fields["Hidden Page"].Value != "1" && c.TemplateID.ToString() != "{5525F19B-B6E9-468E-934B-C4420C50B841}")
       .ToList();

    SiteNavThirdLevelRepeater.DataSource = fourthTierNavItems;
    SiteNavThirdLevelRepeater.DataBind();
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法:

<ItemTemplate>
    <%# GetURL(Eval("myValue").ToString()) %>
</ItemTemplate>

在代码背后。确保方法为public

public string GetURL(string input)
{
    return "It works!";
}

或者,如果该方法不在页面本身的.cs上,但在另一个类中,则需要提供完整的命名空间。

<%# Project1.Class.GetURL(Eval("myValue").ToString()) %>