How to remove menu items from ASP.Net menu control

by Admin 22. March 2011 03:15

In this post I will show code snippet for removing a menu item from ASP.Net Menu control. This kind of situation may arise when you want to authorize certain menu items based on some run time condition or user's role. One option is that you can add entries to Menu control at run time based on authorization rules. And then you have option of removing menu items if you have created your whole menu structure declartively.

Here is code snippet that shows how you can remove menu items from collection.

void AuthorizeMenuNavigation()
{
   List<MenuItem> itemsToRemove = new List<MenuItem>();
   foreach (MenuItem menuitem in NavigationMenu.Items)
   {
      if (menuitem.Value == "Manage")
      {
        itemsToRemove.Add(menuitem);
      }
   }

   foreach (var item in itemsToRemove)
   {
      NavigationMenu.Items.Remove(item);
   }
}

Views: 2257

Tags: ,

ASP.Net | ASP.Net

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli