Hide an ASP.NET Menu Item

只愿长相守 提交于 2020-01-21 06:37:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!