[RESOLVED] Removing a sitemapnode

I am developing an ASP.NET web page with C#.  I have a sitemap document but I want to hide certain nodes based on the user's credentials.  However, I am having problems with my code removing the actual node.  Visual Studio runs the program with no errors.  However, the node is still there.  Below is the code I have developed for this.  The node changes text to "it worked" as I hope written.  However, it will not remove itself.  I can get the item to be disabled, change enablement, but I need to hide it completely from the viewers view - not the document.  Could someone help me with this?

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
    {


        foreach(char f in e.Item.Text)
        {
            if (e.Item.Text == "Student Verification")
            {
                try
                {
                    myMenuItem = e.Item;
                    myMenuItem.Text = "It Worked";
                    Menu1.Items.Remove(myMenuItem);
                }
                catch (Exception ex)
                {
                    string exstring = Convert.ToString(ex);
                }
            }


        }
    }


 

Here is the code that worked for me


<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                        if (e.Item.Parent != null)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                        {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                            e.Item.Parent.ChildItems.Remove(e.Item);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                        }</div> <div></div>

if (e.Item.Parent != null)
{
      e.Item.Parent.ChildItems.Remove(e.Item);
}


I used this in a scenario where I check whether the sitemapnode has an attribute "visible".
If so, and its value = "false", then I would remove the node. 

Another way to do this is on the Page_Load event where the menu resides (master page load event if it is on a master page).  Check to see if the user IsAuthenticated and then based on that conditional statement set the provider of the sitemapdatasource to a different provider in the web.config which points to a sitemap file for authenticated, as well as anonymous.  It's a cleaner way then coding to remove items.  Just my opinion.

Another solution, code free, is to use authorization, where you define who can view individual pages; you can do this per user, or preferably, per role. If you then enable SecurityTrimming on the site map provider, the nodes that a user isn't authorised for will be automatically removed from the site map before display.

See the QuickStarts at http://quickstarts.asp.net/QuickStartv20/aspnet/doc/navigation/default.aspx for more.


it works! Here is my code now.

 

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
    {
        {
            foreach (char f in e.Item.Text)
            {
                if (e.Item.Text == "Student Verification")
                {
                    try
                    {
                        myMenuItem = e.Item;
                        myMenuItem.Text = "It Worked";
                        //Menu1.Items.Remove(myMenuItem);
                        if (e.Item.Parent != null)
                        {
                            e.Item.Parent.ChildItems.Remove(e.Item);
                        }


                    }
                    catch (Exception ex)
                    {
                        string exstring = Convert.ToString(ex);
                    }
                }


            }
       }
}


 

thoughts on "[RESOLVED] Removing a sitemapnode"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>