views:

2223

answers:

3

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?

A: 

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.

Slavo
Basically I want to show 3 menu items ie.UsersCreare UserI want to include "Edit User" to the sitemap but not on the TreeView.
gugulethun
I still don't understand what you want.
Slavo
+1  A: 

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);
}
Paul Suart
A: 

Very nice tip with a custom attribute, thanks !

scra