Asp.Net: Menu control remove MenuItem (MenuItemDataBound)

For a project I needed to remove menu items to the pages in the folder ‘Subscriberpages’ when a the user is in the Role of ‘Marketing’ and ‘AccountManagement’. To do this I added the following code to the MenuItemDataBound event.


protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
 SiteMapNode node = e.Item.DataItem as SiteMapNode;

 if (node.Url.Contains("<em>Subscriberpages</em>"))
 {
  if (HttpContext.Current.User.IsInRole("<em>Marketing</em>") || HttpContext.Current.User.IsInRole("<em>AccountManagement'</em>"))
  {
   // Get menu.
   Menu topMenu = (Menu)sender;

   // Remove dataitem from menu.
   topMenu.Items.Remove(e.Item);
  }
 }
}

To generate the menu I used the Web.sitemap and the asp:Menu control.

Leave a Reply

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