如何隐藏主页上的 SiteMapPath 根节点?
当用户位于根节点页面时,如何隐藏 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
一种可能的解决方案是简单地隐藏主页上的
SiteMapPath
控件:One possible solution would be to simply hide the
SiteMapPath
control on the home page:我见过一些基于代码的示例,但这里有一个廉价的 CSS 解决方案(尽管你的目标浏览器必须支持 css 2.1),它将隐藏根节点和以下路径分隔符。
通过将 RootNodeTemplate 设置为空来杀死根节点,如下所示:
这将使其不为根节点渲染任何内容,但根的路径分隔符仍将显示,因此将这些 CSS 选择器添加到您的样式表中(重要提示:注意我给了我的SiteMapPath1 元素是一个名为“breadCrumbTrail”的 CssClass):
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:
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'):
我设法解决了这个问题,但花了一段时间,因为我遇到的问题有点微妙。 schou-rode 的想法是正确的,这就是我在 Page_Load 中所做的事情,但没有成功。 它不起作用的原因是我在
SiteMapResolve
中克隆节点并返回克隆。 这发生在Page_Load
之前,因此SiteMap.CurrentNode
引用了克隆,并且与SiteMap.RootNode
的比较失败。这是完整的解决方案:
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 inSiteMapResolve
and returning the clone. This occurred beforePage_Load
soSiteMap.CurrentNode
referenced the clone and the comparison toSiteMap.RootNode
failed.Here's the full solution:
SiteMapDataSource 上还有一个 ShowStartingNode 属性。 将其设置为 false 以隐藏根节点。
There is also a ShowStartingNode property on the SiteMapDataSource. Set this to false to hide the root node.
在主页上,将以下脚本添加到“head”部分:
要应用上述方法,应将
SiteMapPath1
放置在 MasterPage 上。On the home page, add the script below to the "head" part:
To apply the above method,
SiteMapPath1
should be placed on the MasterPage.通过 3 个简单步骤隐藏 SiteMapPath 根注释的正确方法之一:
从 ContentPage 引用 MasterPage
示例:
在设计器类中将 SiteMapPath 设置为受保护的内部
示例:
从 ContentPage 中隐藏它
示例:
The one of the right way to hide SiteMapPath root note in 3 easy steps:
Reference MasterPage from ContentPage
Example:
Make SiteMapPath as Protected Internal in designer class
Example:
Hide it from ContentPage
Example:
我将我的插入到我的 _Layout.cshtml 中,发现最简单的解决方案是在控件渲染块周围包装一个 If 语句(带有之前建议的逻辑),然后就到此为止:
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:
和CSS代码:
and css code :
最近我遇到了类似的问题,但我在解决方案中使用 XmlDataSource 作为菜单。
源 XML 的示例结构:
如果您不想显示“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:
If you want to NOT display 'root' menu item, you have to simple set XPath property on the XmlDataSource to value '/root/*'
ParentLevelsDisplayed=0 会有帮助
ParentLevelsDisplayed=0 will help