Reputation: 107
I discovered a kind of strange behavior in my applicataion.
I have a menu, which is populated by a siteMap.
<asp:SiteMapDataSource ID="siteMapDataSource" runat="server" SiteMapProvider="examGenSiteMap" />
<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapDataSource" Orientation="Horizontal" StaticDisplayLevels="2" OnMenuItemDataBound="MenuExamGen_MenuItemDataBound">
// Some Item styles
</asp:Menu>
Now I have added a OnMenuItemDataBound in which I check, if the roles in the siteMap matches with the user role. If not: remove that item, so the user can't see it.
if (!((SiteMapNode)e.Item.DataItem).Roles.Contains(Session["Role"].ToString()))
{
mainMenu.Items.Remove(e.Item);
}
I debugged this code multiple times, it works fine. All the items I want to remove are identified by the if and the Remove() function gets those items as argument.
The only problem: On my webpage all items ar still shown, and I have no idea why.
Upvotes: 1
Views: 702
Reputation: 107
Okay, I have solved the problem. I don't know, why
mainMenu.Items.Remove(e.Item);
is not working anymore. You need to remove child elements of the root element.
System.Web.UI.WebControls.Menu mainMenu = (System.Web.UI.WebControls.Menu)sender;
if (!((SiteMapNode)e.Item.DataItem).Roles.Contains(Session["Role"].ToString()))
{
mainMenu.Items[0].ChildItems.Remove(e.Item);
}
But thanks for trying to help me :)
Upvotes: 1