如何隐藏主页上的 SiteMapPath 根节点?

发布于 2024-07-25 09:20:23 字数 336 浏览 6 评论 0原文

当用户位于根节点页面时,如何隐藏 SiteMapPath 控件中的根节点? 例如,我在子页面上的面包屑路径是:

首页 > 产品展示> 锤子> 圆珠笔

挺好的。 但是当用户位于主页上时,SiteMapPath 控件会显示

首页

里面都是无用的杂乱。 我想当用户位于主页时禁止显示主页(根节点)。 我在母版页中有 SiteMapPath 控件。 另外,我正在处理 SiteMapResolve 以设置节点中的查询字符串。

How can I hide the root node in a SiteMapPath control when the user is on the root node page? For example, my breadcrumb trail on a child page is:

Home > Products > Hammers > Ball Peen

which is fine. But when the user is on the Home page, the SiteMapPath control displays

Home

which is useless clutter. I want to suppress displaying Home (the root node) when the user is on the home page. I have the SiteMapPath control in a master page. Also, I'm handling SiteMapResolve to set the querystrings in the nodes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

困倦 2024-08-01 09:20:23

一种可能的解决方案是简单地隐藏主页上的 SiteMapPath 控件:

mySiteMapPath.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);

One possible solution would be to simply hide the SiteMapPath control on the home page:

mySiteMapPath.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
ぶ宁プ宁ぶ 2024-08-01 09:20:23

我见过一些基于代码的示例,但这里有一个廉价的 CSS 解决方案(尽管你的目标浏览器必须支持 css 2.1),它将隐藏根节点和以下路径分隔符。

通过将 RootNodeTemplate 设置为空来杀死根节点,如下所示:

<asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="breadCrumbTrail">
   <RootNodeTemplate></RootNodeTemplate>
</asp:SiteMapPath>

这将使其不为根节点渲染任何内容,但根的路径分隔符仍将显示,因此将这些 CSS 选择器添加到您的样式表中(重要提示:注意我给了我的SiteMapPath1 元素是一个名为“breadCrumbTrail”的 CssClass):

.breadCrumbTrail
{
  font-size: small;
}

/*
First child element rendered by a SiteMapPath is an <a> tag you have no control over, 
adjacent to that is your root node's span tag, adjacent to that is the root node's 
path-separator span: don't display it.
*/
.breadCrumbTrail > a:first-child + span + span
{
  display: none;
}

I've seen some code-based examples, but here's a cheep CSS solution (your target browsers must support css 2.1 though) that will hide the root node and the following path-separator.

Kill the Root node by setting the RootNodeTemplate to empty like so:

<asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="breadCrumbTrail">
   <RootNodeTemplate></RootNodeTemplate>
</asp:SiteMapPath>

That will make it render nothing for the Root node, but the Root's path-separator will still be displayed, so add these CSS selectors to your stylesheet (Important: notice I gave my SiteMapPath1 element a CssClass named 'breadCrumbTrail'):

.breadCrumbTrail
{
  font-size: small;
}

/*
First child element rendered by a SiteMapPath is an <a> tag you have no control over, 
adjacent to that is your root node's span tag, adjacent to that is the root node's 
path-separator span: don't display it.
*/
.breadCrumbTrail > a:first-child + span + span
{
  display: none;
}
以酷 2024-08-01 09:20:23

我设法解决了这个问题,但花了一段时间,因为我遇到的问题有点微妙。 schou-rode 的想法是正确的,这就是我在 Page_Load 中所做的事情,但没有成功。 它不起作用的原因是我在 SiteMapResolve 中克隆节点并返回克隆。 这发生在 Page_Load 之前,因此 SiteMap.CurrentNode 引用了克隆,并且与 SiteMap.RootNode 的比较失败。

这是完整的解决方案:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    if (SiteMap.CurrentNode == null || SiteMap.CurrentNode == SiteMap.RootNode)
    {
        return SiteMap.CurrentNode;
    }
    // clone and set querystring in other nodes...
}

I managed to figure this out but it took a while because the problem I was having was somewhat subtle. schou-rode has the right idea and that is what I was doing in Page_Load without success. The reason it wasn't working is because I was cloning the node in SiteMapResolve and returning the clone. This occurred before Page_Load so SiteMap.CurrentNode referenced the clone and the comparison to SiteMap.RootNode failed.

Here's the full solution:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    if (SiteMap.CurrentNode == null || SiteMap.CurrentNode == SiteMap.RootNode)
    {
        return SiteMap.CurrentNode;
    }
    // clone and set querystring in other nodes...
}
我为君王 2024-08-01 09:20:23

SiteMapDataSource 上还有一个 ShowStartingNode 属性。 将其设置为 false 以隐藏根节点。

There is also a ShowStartingNode property on the SiteMapDataSource. Set this to false to hide the root node.

悍妇囚夫 2024-08-01 09:20:23

在主页上,将以下脚本添加到“head”部分:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath sp = (SiteMapPath)Master.FindControl("SiteMapPath1");
    sp.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

要应用上述方法,应将 SiteMapPath1 放置在 MasterPage 上。

On the home page, add the script below to the "head" part:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath sp = (SiteMapPath)Master.FindControl("SiteMapPath1");
    sp.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

To apply the above method, SiteMapPath1 should be placed on the MasterPage.

眼眸印温柔 2024-08-01 09:20:23

通过 3 个简单步骤隐藏 SiteMapPath 根注释的正确方法之一:

  • 从 ContentPage 引用 MasterPage

    示例:

    <%@ MasterType VirtualPath="~/Master.master" %> 
      
  • 在设计器类中将 SiteMapPath 设置为受保护的内部

    示例:

    受保护的内部全局::System.Web.UI.WebControls.SiteMapPath SiteMapPath1; 
      
  • 从 ContentPage 中隐藏它

    示例:

    Master.SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode); 
      

The one of the right way to hide SiteMapPath root note in 3 easy steps:

  • Reference MasterPage from ContentPage

    Example:

    <%@ MasterType VirtualPath="~/Master.master" %>
    
  • Make SiteMapPath as Protected Internal in designer class

    Example:

    protected internal global::System.Web.UI.WebControls.SiteMapPath SiteMapPath1;
    
  • Hide it from ContentPage

    Example:

    Master.SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
    
乙白 2024-08-01 09:20:23

我将我的插入到我的 _Layout.cshtml 中,发现最简单的解决方案是在控件渲染块周围包装一个 If 语句(带有之前建议的逻辑),然后就到此为止:

@if (SiteMap.CurrentNode != SiteMap.RootNode)
{
  @Html.MvcSiteMap().SiteMapPath()
}

I had mine plugged into my _Layout.cshtml and found the easiest solution was to wrap an If statement (with previously suggested logic) around the control rendering block and call it a day:

@if (SiteMap.CurrentNode != SiteMap.RootNode)
{
  @Html.MvcSiteMap().SiteMapPath()
}
满身野味 2024-08-01 09:20:23
<asp:SiteMapPath ID="contentNavigation" runat="server">
    <RootNodeTemplate>
    </RootNodeTemplate>
</asp:SiteMapPath>

和CSS代码:

#ctl00_contentNavigation span:nth-child(2),span:nth-child(3)
{
    display:none;
}
<asp:SiteMapPath ID="contentNavigation" runat="server">
    <RootNodeTemplate>
    </RootNodeTemplate>
</asp:SiteMapPath>

and css code :

#ctl00_contentNavigation span:nth-child(2),span:nth-child(3)
{
    display:none;
}
以为你会在 2024-08-01 09:20:23

最近我遇到了类似的问题,但我在解决方案中使用 XmlDataSource 作为菜单。

源 XML 的示例结构:

<root>
  <Menu text="" url=""/>
  <Menu text="" url=""/>
</root>

如果您不想显示“root”菜单项,则必须简单地将 XmlDataSource 上的 XPath 属性设置为值“/root/*”

Recently I had similar problem, but I am using XmlDataSource for the menu in my solution .

Sample structure of the source XML:

<root>
  <Menu text="" url=""/>
  <Menu text="" url=""/>
</root>

If you want to NOT display 'root' menu item, you have to simple set XPath property on the XmlDataSource to value '/root/*'

残花月 2024-08-01 09:20:23

ParentLevelsDisplayed=0 会有帮助

<asp:SiteMapPath ID="SiteMapPath1" runat="server" PathSeparator="" 
        ParentLevelsDisplayed="0" >
        <RootNodeTemplate></RootNodeTemplate>            
    </asp:SiteMapPath>

ParentLevelsDisplayed=0 will help

<asp:SiteMapPath ID="SiteMapPath1" runat="server" PathSeparator="" 
        ParentLevelsDisplayed="0" >
        <RootNodeTemplate></RootNodeTemplate>            
    </asp:SiteMapPath>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文