Chris N.P.
Chris N.P.

Reputation: 783

How to Show/Hide Menu Item and how to create it at run time?

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?

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

Please help me with this.

Thank you.

Upvotes: 0

Views: 7353

Answers (2)

Simnix
Simnix

Reputation: 39

you can use:

menuItem1.Visible = true;

I use this in my applications if needed.

private void Button1_Click(object sender, EventArgs e)
{
    MenuItem1.Visible = true;
}

private void Button2_Click(object sender, EventArgs e)
{
    MenuItem1.Visible = false;
}

Change MenuItem1 for the name of the menu item.

Upvotes: 1

Sean Petiya
Sean Petiya

Reputation: 71

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

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 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"));
    }
}

Upvotes: 2

Related Questions