1

i'm trying to set a sitemap based in roles/users.. ( i can't use securitytrimming because the role membership provider, i have it in the server side and it'd be complicated to implement ) so what i'm trying to do is to do it simply after getting the item to remove and do it.

I have a website map defined as:

<siteMapNode title="Gestion des roles" url="" description="Gestion des roles">
  <siteMapNode url="~/Membership/AddRole.aspx" title="Ajouter Role" description="Ajouter role" />
  <siteMapNode url="~/Membership/DeleteRole.aspx" title="Supprimer Role" description="Supprimer un role" />
</siteMapNode>

<siteMapNode title="Gestion des sites" url="" description="Gestion des sites">
  <siteMapNode url="~/Membership/AddSite.aspx" title="Ajouter Site" description="Ajouter un nouveau site" />
</siteMapNode>

the way i'm accessing now is with this code:

Menu menu = (Menu)Master.FindControl("Menu1");
String valuePath = @"Gestions/Gestion/Ajouter";
MenuItem item = menu.FindItem(valuePath);
if (item.Parent != null)
item.Parent.ChildItems.Remove(item);

but after executing the item is null and an exception is thrown. thank you for reading

Soufiane N
  • 113
  • 1
  • 11
  • It could be a lot easier if you implement CustomSiteMapProvider by overriding SiteMapProvider. – Win May 13 '13 at 16:12
  • All my roles/users are implemented with a provider in the server side, now i'm trying to call some methods exposed by the wcf data service and test if the current user has the right to see some nodes or not! i dont know if it's possible to implement another provider with same roles/users stored in my only data base. thats why i'm trying to find a correct way of the call i wrote above.. – Soufiane N May 13 '13 at 20:58

2 Answers2

0

You can find a menu item based on Url using this recursive function like this -

private MenuItem FindItem(MenuItemCollection collection, string url)
{
    foreach (MenuItem item in collection)
    {
        if (item.NavigateUrl.Equals(url, 
            StringComparison.InvariantCultureIgnoreCase))
            return item;

        if (item.ChildItems.Count > 0)
            return FindItem(item.ChildItems, url);
    }

    return null;
}

protected void Page_Load(object sender, EventArgs e)
{
    var menu = (Menu)Master.FindControl("NavigationMenu");
    // string valuePath = @"Gestions/Gestion/Ajouter";
    string valuePath = @"~/About/About2.aspx";

    var item = FindItem(menu.Items, valuePath);

    if(item != null)
    {
        if (item.Parent != null)
            item.Parent.ChildItems.Remove(item);
        else
            menu.Items.Remove(item);
    }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • Thank you for your code win, i dont know but when i tried to implement this code and debugged it. the var menu is null and item as well. the value navigationmenu you've passed refers to the name of the menu or the name of the div ? because i tried to pass "Menu1" but no way. – Soufiane N May 14 '13 at 09:27
  • `NavigationMenu` is a menu control. There is another way of accessing a control inside master page by creating a getter and setter for `Menu` inside the master page. http://stackoverflow.com/a/5207566/296861 – Win May 14 '13 at 14:00
0

If you encounter problems finding items in the MENU because of Data Binding issue, you can use ASP.NET PreRenderComplete :

   protected void Page_PreRenderComplete(object sender, EventArgs e){}
KrishnaDhungana
  • 2,604
  • 4
  • 25
  • 37