必要时从 Repeater 中的代码隐藏中引用函数以获取不同的项目 URL

Reference a function from codebehind inside Repeater to get a different item URL if necessary

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

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 到 return。我希望也许我可以在设置 link 的转发器中以某种方式使用此功能。目前,我只是获取该项目的当前 link,没有别的:

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

你使用的方法是这样的:

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

并且在代码后面。确保方法是 public

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

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

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