nhat
nhat

Reputation: 1169

Possible to created a submenu programmatically in c#?

On my page theres buttons on the top. I would like to add sub menus to those buttons:

protected void AddMenuItems()
{
    // Check privledges and add links to admin pages accordingly.
    // List adds pages in order of user precedence.


    if (_cu.IsUser)
    {
       NavigationMenu.Items.Add(new MenuItem("Generate UserIDs", "Generate UserIDs", null, "~/GenPrefixList.aspx"));
    }
}

Is there a way I can add a sub menu to the item?

Upvotes: 1

Views: 2730

Answers (1)

Devin Burke
Devin Burke

Reputation: 13820

Yes, each MenuItem has a ChildItems collection to which you can add an indefinite number of items.

protected void AddMenuItems()
{
    // Check privledges and add links to admin pages accordingly.
    // List adds pages in order of user precedence.


    if (_cu.IsUser)
    {
       var newItem = new MenuItem("Generate UserIDs", "Generate UserIDs", null, "~/GenPrefixList.aspx");
       var subItem = new MenuItem("Sub Menu Item", "Submenu Item", null, "page.aspx");
       newItem.ChildItems.Add(subItem);
       NavigationMenu.ChildItems.Add(newItem);
    }
}

The "Sub Menu Item" will be a child of "Geneate UserIDs", which is a child of the navigation menu.

Update: The Items reference in your code should be ChildItems. Items is the collection used for the Windows application menu; you added the ASP.NET tag so I assume this is a web application.

Upvotes: 1

Related Questions