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

4

2 回答 2

0

如果由于数据绑定问题而在 MENU 中查找项目时遇到问题,可以使用 ASP.NET PreRenderComplete

   protected void Page_PreRenderComplete(object sender, EventArgs e){}
于 2013-05-14T02:51:50.180 回答
0

您可以使用这样的递归函数找到基于 Url 的菜单项 -

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);
    }
}
于 2013-05-13T22:28:55.723 回答