问题
I have an ASP.NET webforms application with a Menu control. How does one hide a particular menu item via code? I've seen a few articles pointing out how to do it with ASP.Net membership/roles-based security, but this particular use case has nothing to do with that. I simply need a way to programmatically remove a menu item from code. Any help would be appreciated.
回答1:
It would be more straight forward to use
myMenu.Items.RemoveAt(0);
This will remove the first menuitem
myMenu.Items[0].ChildItems.RemoveAt(1);
This will remove the second child of the fist menuitem
myMenu.Items[0].ChildItems[1].ChildItems.RemoveAt(1)
This will remove the second child of the second child of the fist menuitem
回答2:
Doh! Ok, I figured it out. The correct syntax is (VB.Net):
mnuMyMenu.Items.Remove(mnuMyMenu.Items(1))
回答3:
myMenu.Items(0).ChildItems.Remove(myMenu.Items(0).ChildItems(1))
回答4:
If you want to remove a menu item by the menu item Text property, you can use:
myMenu.Items.Remove(myMenu.FindItem("Item Text"))
来源:https://stackoverflow.com/questions/430573/hide-an-asp-net-menu-item