如何显示/隐藏菜单项以及如何在运行时创建它?

时间:2023-02-05 10:35:41

I have this menu:

我有这个菜单:

<asp:Menu
        id="Menu1"
        Orientation="Horizontal"
        StaticMenuItemStyle-CssClass="tab"
        StaticSelectedStyle-CssClass="selectedTab"
        CssClass="tabs"
        OnMenuItemClick="Menu1_MenuItemClick"
        Runat="server">
        <Items>
        <asp:MenuItem Text="Tab 1" Value="0" Selected="true" />
        <asp:MenuItem Text="Tab 2" Value="1" />
        <asp:MenuItem Text="Tab 3" Value="2" />
        <asp:MenuItem Text="Tab 4" Value="3" />
        <asp:MenuItem Text="Tab 5" Value="4" />
        <asp:MenuItem Text="Tab 6" Value="5" />
        <asp:MenuItem Text="Tab 7" Value="6" />
        <asp:MenuItem Text="Tab 8" Value="7" />
        <asp:MenuItem Text="Tab 9" Value="8" />
        <asp:MenuItem Text="Tab 10" Value="9"/>
        </Items>    
    </asp:Menu>

Is it possible to hide one of the menu item like say "tab 7" when I click on a button and show it again when I click on another button? I understand that I can use "RemoveAt" but how can I show it again after that?

当我点击一个按钮并单击另一个按钮时再次显示时,是否可以隐藏其中一个菜单项,例如“tab 7”?我知道我可以使用“RemoveAt”但是之后如何再次显示呢?

I want to know as well how to create this kind of menu structure at run time.

我想知道如何在运行时创建这种菜单结构。

Please help me with this.

请帮我解决一下这个。

Thank you.

谢谢。

1 个解决方案

#1


2  

You can use the RemoveAt() method knowing the index of the item you want to remove.

您可以使用RemoveAt()方法知道要删除的项目的索引。

Menu.Items.RemoveAt(6);

There is no Hide() method or Visible property on the MenuItem class. But you can create a new instance and add it to the menu during your second button's click event.

MenuItem类上没有Hide()方法或Visible属性。但是,您可以在第二个按钮的单击事件期间创建一个新实例并将其添加到菜单中。

    MenuItem myItem = new MenuItem("Tab 7", "6");
    Menu.Items.AddAt(6, myItem);

You can use the same idea to build the menu with code:

您可以使用相同的想法来构建包含以下代码的菜单:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Menu.Items.Add(new MenuItem("Tab 1", "0"));
        Menu.Items.Add(new MenuItem("Tab 2", "1"));
        ...
        Menu.Items.Add(new MenuItem("Tab 10", "9"));
    }
}

#1


2  

You can use the RemoveAt() method knowing the index of the item you want to remove.

您可以使用RemoveAt()方法知道要删除的项目的索引。

Menu.Items.RemoveAt(6);

There is no Hide() method or Visible property on the MenuItem class. But you can create a new instance and add it to the menu during your second button's click event.

MenuItem类上没有Hide()方法或Visible属性。但是,您可以在第二个按钮的单击事件期间创建一个新实例并将其添加到菜单中。

    MenuItem myItem = new MenuItem("Tab 7", "6");
    Menu.Items.AddAt(6, myItem);

You can use the same idea to build the menu with code:

您可以使用相同的想法来构建包含以下代码的菜单:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Menu.Items.Add(new MenuItem("Tab 1", "0"));
        Menu.Items.Add(new MenuItem("Tab 2", "1"));
        ...
        Menu.Items.Add(new MenuItem("Tab 10", "9"));
    }
}