简体   繁体   English

如何将现有菜单项添加回已删除的ASP.NET C#

[英]How To Add An Existing Menu Item Back Once It Has Been Removed ASP.NET C#

I am trying to add back an menu item once it has been removed. 我试图添加一个菜单项一旦被删除。 I have removed the item using the following code: 我已使用以下代码删除了该项目:

NaviMenu.Items.Remove(NaviMenu.Items[1]);

The ASP.NET project was created on the framework 4.5, and the project was also created in Visual Studio 2012. ASP.NET项目是在框架4.5上创建的,并且该项目也在Visual Studio 2012中创建。

The control is a ASP.NET menu control System.Web.UI.WebControls.Menu not to be confused with System.Windows.Forms.ContextMenuStrip Web menus and Windows forms menu are not the same, and have different properties. 该控件是一个ASP.NET菜单控件System.Web.UI.WebControls.Menu ,不要与System.Windows.Forms.ContextMenuStrip混淆。Web菜单和Windows窗体菜单并不相同,并且具有不同的属性。

I would also like to have the item restored back to the original position. 我还想将项目恢复到原始位置。 The menu item is already in the menu by default. 默认情况下,菜单项已在菜单中。 Is there anyway to restore the menu item with out having to re add everything back like the Navigation URL, Vaule, etc? 无论如何,是否有还原菜单项而无需重新添加所有内容(如导航URL,Vaule等)的信息?

The idea is that menu items are removed when the page is loaded. 这个想法是在加载页面时删除菜单项。 After the user logs into the site, then some of the items in the menu would be added back for the user. 用户登录站点后,菜单中的某些项目将重新添加给用户。

Edit: I was going down the wrong path, so I'm replacing the entire answer. 编辑:我走错了路,所以我要替换整个答案。

Ok, it does seem that System.Web.Ui.WebControls.MenuItem does not include an option to hide/unhide, although Menu does. 好的,尽管Menu包含,但System.Web.Ui.WebControls.MenuItem似乎没有包含用于隐藏/取消隐藏的选项。 This is really dumb. 这真是愚蠢。

There is no easy answer, and there are different ways to implement this. 没有简单的答案,并且有不同的实现方法。 The main thing is that you're going to need to: 最主要的是,您将需要:

1) Keep a hold on the menu item object that you are removing. 1)按住要删除的菜单项对象。 You can't only reference the object from the menu 您不仅可以从菜单中引用对象

2) Keep track of where in the menu each item goes. 2)跟踪每个项目在菜单中的位置。

So, my new strategy is this ... you should have a method responsible for creating your menu. 因此,我的新策略是...您应该有一个负责创建菜单的方法。 It'll be like this ... 就像这样...

public class MyMenuItem
{
    public bool ShowWhenNotLoggedIn { get; set; }
    public System.Web.UI.WebControls.MenuItem MenuItem { get; set; }
}

List<MyMenuItem> MyMenuItems = new List<MyMenuItem>(); //Create all your menu items and put them in here before running the menthod below
void CreateMyMenu(bool IsUserLoggedIn)
{
    MyMenu.Items.Clear();
    foreach (MyMenuItem item in MyMenuItems)
    {
        if (item.ShowWhenNotLoggedIn || IsUserLoggedIn)
            MyMenu.Items.Add(item.MenuItem);
    }
}

So, what I like about this is that you don't constantly have to remember what order the items go in. You just clear and re-create the whole menu each time. 因此,我对此的喜欢是,您不必经常记住这些项目的输入顺序。您只需每次清除并重新创建整个菜单即可。 It's slightly less efficient than only removing/adding the items where you want them, but it's easier to read and understand the code. 与仅在需要的地方删除/添加项目相比,它的效率略低,但更易于阅读和理解代码。 I think the performance hit is worth making the code readable, but that's your decision to make. 我认为对性能的影响值得使代码更具可读性,但这是您的决定。

Also note that this is sort of like State Pattern-lite. 另请注意,这有点像State Pattern-lite。 We've defined two states: Logged In and Not Logged In. 我们定义了两种状态:“已登录”和“未登录”。 We've defined those states on objects that are affected by those states (the menu items). 我们已经在受那些状态(菜单项)影响的对象上定义了这些状态。 So, once you've written this logic, you don't have to dwell on each menu item anymore. 因此,一旦编写了此逻辑,就不必再停留在每个菜单项上了。 You just say declare that you're in a new state, and everybody updates themselves accordingly. 您只需说出自己处于新状态,每个人都会相应地更新自己。 If you have other logic that depends on logged in vs not logged in, you can follow this same thing to them. 如果您还有其他逻辑取决于登录和未登录,则可以对他们遵循同样的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮(ASP.NET c#)后,如何在ListView中访问控件? - How can i access a control within a ListView once a button has been clicked (ASP.NET c#)? ASP.net C# ConnectionString 属性尚未初始化 - ASP.net C# The ConnectionString property has not been initialized 在c#asp.net中单击了哪些HyperLink? - What HyperLink has been clicked in c# asp.net? 如何在asp.net C#代码后面的菜单控件中禁用特定菜单项 - How to disable specific menu item from menu control in asp.net C# code behind 在asp.net c中为菜单项动态设置NavigateUrl# - Dynamically set NavigateUrl for a menu item in asp.net c# 使用UpdatePanel删除行后,更新/刷新Gridview。 ASP.NET C#AJAX - Updating/Refreshing a Gridview once a row has been deleted Using UpdatePanel. ASP.NET C# AJAX 如何知道已请求哪个图像C#,ASP.Net - how to know which Image has been requested C#,ASP.Net 在单击按钮时,asp.net中的Sharepoint集合尚未初始化-C#asp.net - Sharepoint collection has not been initialized in asp.net on button click - C# asp.net 向 asp.net c# 中的现有 WebService 添加 Kerberos 身份验证 - Add a Kerberos authentication to existing WebService in asp.net c# 如何仅将项目添加到以前未添加的列表中? (C# - ASP.NET) - How to only add items to a list that haven't been added before? (C# - ASP.NET)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM