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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
Method 2
Doh! Ok, I figured it out. The correct syntax is (VB.Net):
mnuMyMenu.Items.Remove(mnuMyMenu.Items(1))
Method 3
myMenu.Items(0).ChildItems.Remove(myMenu.Items(0).ChildItems(1))
Method 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"))
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0