Visual Studio .NET - Add menu items programatically

Asked By Ghaffar Abdul on 20-Mar-07 12:18 AM

Hello All..I am facing a problem with menu control.

I want to add menu items and sub menu items programatically. I am able to add sub menu items, when i add menu items from the menu editor as:

Menu1.Items[0].ChildItems.Add(new MenuItem("View ", "", "", "~/view.aspx"));

What i want to do is to add menu items programatically not using the menu editor, i tried the following but not working.

Menu Menu1 = new Menu();

Menu1.Items[0].Text = "Item1";

Menu1.Items[0].Value = "1";

Regards

Abdul Ghaffar



To build a menu programmatically, - mv ark replied to Ghaffar Abdul on 20-Mar-07 04:33 AM

understand & adapt this snippet from MSDN - http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menu.aspx

<script runat="server">
    
  void Page_Load(Object sender, EventArgs e)
  {
    
    // Create a new Menu control.
    Menu newMenu = new Menu();
          
    // Set the properties of the Menu control.
    newMenu.ID = "NavigationMenu";
    newMenu.DisappearAfter = 2000;
    newMenu.Orientation = Orientation.Vertical;
    newMenu.Target = "_blank";
    newMenu.StaticMenuItemStyle.ForeColor = System.Drawing.Color.Black;
    newMenu.DynamicMenuItemStyle.ForeColor = System.Drawing.Color.Gray;
    
    // Create the menu structure.
    
    // Create the root menu item.
    MenuItem homeMenuItem;
    homeMenuItem = CreateMenuItem("Home", "Home.aspx", "Home");
    
    // Create the submenu items.
    MenuItem musicSubMenuItem;
    musicSubMenuItem = CreateMenuItem("Music", "Music.aspx", "Music");
    
    MenuItem moviesSubMenuItem;
    moviesSubMenuItem = CreateMenuItem("Movies", "Movies.aspx", "Movies");
    
    // Add the submenu items to the ChildItems
    // collection of the root menu item.
    homeMenuItem.ChildItems.Add(musicSubMenuItem);
    homeMenuItem.ChildItems.Add(moviesSubMenuItem);
    
    // Add the root menu item to the ItemsCollection
    // of the Menu control.
    newMenu.Items.Add(homeMenuItem);
    
    // Add the Menu control to the Controls collection
    // of the PlaceHolder control.
    MenuPlaceHolder.Controls.Add(newMenu);
    
  }
 
  MenuItem CreateMenuItem(String text, String url, String toolTip)
  {
    
    // Create a new MenuItem object.
    MenuItem menuItem = new MenuItem();
    
    // Set the properties of the MenuItem object using
    // the specified parameters.
    menuItem.Text = text;
    menuItem.NavigateUrl = url;
    menuItem.ToolTip = toolTip;
    
    return menuItem;
    
  }

</script>

Great .... Thanks MV .... You solve an aching problem of mine .... - Ghaffar Abdul replied to mv ark on 20-Mar-07 05:27 AM

Once again thanks ....

Regards

Ghaffar

Great .... Thanks MV .... You solved an aching problem of mine .... - Ghaffar Abdul replied to mv ark on 20-Mar-07 05:28 AM

Once again thanks ....

Regards

Ghaffar

Fouad replied to Ghaffar Abdul on 05-May-10 05:52 AM
thank you for this simple wonderful example.
:)