在 ASP.NET 中动态添加/删除菜单项?
我有一个菜单控件(Menu1),我想根据我在数据库中存储的有关经过身份验证的用户的某些信息从菜单中添加/删除项目。我不确定如何从菜单控件访问特定菜单项并在运行时删除它们?
I have a menu control (Menu1) and I want to add/remove items from the menu based on certain information I have stored about the authenticated user in the database. I'm not sure though how to access specific menu items from the menu control and remove them at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASP.NET 菜单可以通过隐藏代码访问。例如,在标记中声明的菜单具有 id“Menu1”,可以像这样访问:
在该示例中,根据菜单所在的当前页面选择当前选定的菜单项。同样,Items 集合可用于添加或删除单个菜单项。
请注意,在菜单项上,ChildItems 集合可用于更改子菜单项集合。
更多信息:http://msdn。 microsoft.com/en-us/library/system.web.ui.webcontrols.menu.items.aspx
@Edit:使其与问题中的数据更加一致
ASP.NET menus can be accessed via code behind. A menu declared in the markup, having the id 'Menu1' for example, could be accessed like so:
In that example, the currently selected menu item is chosen according to the current page the menu is on. Just as well, the Items collection may be used to add or remove single menu items.
Note, on menu items, the ChildItems collection can be used to alter the submenu items collection.
More infos: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.items.aspx
@Edit: made it more coherent with the data in the question
我希望你能说得更具体,但我知道评论通知在这里有点糟糕。那么,如果您能确切地告诉我
A.删除项目:
Menu1.Items.Remove(Menu1.FindItem("作业"))
B.添加项目:
Menu1.Items.Add(new MenuItem("新闻"))
或者使用它来指定所需的
新添加的项目的属性:
MenuItem item = new MenuItem()
item.NavigateUrl =""
item.Text = "子测试"
Menu1.Items.Add(mnuTestChild)
I wish you was more specific but I know that the notification for comments kinda sucks here. so if you could tell me exactly
A. Remove items:
Menu1.Items.Remove(Menu1.FindItem("Jobs"))
B. Add Items:
Menu1.Items.Add(new MenuItem("News"))
OR use this to specify the required
properties for the new added item:
MenuItem item = new MenuItem()
item.NavigateUrl =""
item.Text = "Child Test"
Menu1.Items.Add(mnuTestChild)