I have a SiteMap with All my nodes. I'm using a TreeView control which is linked to the SiteMap for navigation. Now I would like to hide certain nodes from appearing on the TreeView. Is it possible to do this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, it's definitely possible. The way we do it is to add a custom "IsPhantom" attribute to the nodes we don't want shown in the sitemap (and in various other places too):
<siteMapNode url="~/Welcome.aspx" title="Welcome" description="" isPhantom="true" />
Then in the sitemap control, use the following code to remove nodes that have the "IsPhantom" attribute:
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.TreeNodeDataBound += new TreeNodeEventHandler(TreeView1_TreeNodeDataBound);
SiteMapSource.Provider = this.CurrentProvider;
}
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
SiteMapNode thisMapNode = (SiteMapNode)e.Node.DataItem;
TreeNode parentTreeNode = e.Node.Parent;
if (thisMapNode["isPhantom"] != null && thisMapNode["isPhantom"].ToLower().Equals(bool.TrueString.ToLower()) && parentTreeNode != null)
parentTreeNode.ChildNodes.Remove(e.Node);
}
回答2:
Based on what criteria? If you only want to hide specific single nodes, subscribe to the NodeDataBound event of the TreeView and set the whole item (node) to Visible=false.
If you need to do this in a better way and provide more flexibility, I would advise that you implement your own SiteMapProvider. Then you can have a property ShowInNavigation for each sitemap node, and would be able to set that when constructing your sitemap.