Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Menu Class
 Menu Constructor
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Menu Constructor

Initializes a new instance of the Menu class.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Sub New
Visual Basic (Usage)
Dim instance As New Menu()
C#
public Menu()
Visual C++
public:
Menu()
JScript
public function Menu()

Use this constructor to create and initialize a new instance of the Menu class. To dynamically add a Menu control to a page, create a new Menu object, set its properties, and then add it to the Controls collection of a container control, such as PlaceHolder.

The following code example demonstrates how to use the constructor to dynamically add a Menu control to a page.

Visual Basic
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a new Menu control.
    Dim newMenu As 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.
    Dim homeMenuItem As MenuItem
    homeMenuItem = CreateMenuItem("Home", "Home.aspx", "Home")

    ' Create the submenu items.
    Dim musicSubMenuItem As MenuItem
    musicSubMenuItem = CreateMenuItem("Music", "Music.aspx", "Music")

    Dim moviesSubMenuItem As MenuItem
    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)

  End Sub

  Function CreateMenuItem(ByVal text As String, ByVal url As String, ByVal toolTip As String) As MenuItem

    ' Create a new MenuItem object.
    Dim menuItem As New MenuItem()

    ' Set the properties of the MenuItem object using
    ' the specified parameters.
    menuItem.Text = text
    menuItem.NavigateUrl = url
    menuItem.ToolTip = toolTip

    Return menuItem

  End Function

</script>

<html  >
  <head runat="server">
    <title>Menu Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Menu Constructor Example</h3>

      <asp:placeholder id="MenuPlaceHolder"
        runat="server"/>

    </form>
  </body>
</html>

C#
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>

<html  >
  <head runat="server">
    <title>Menu Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Menu Constructor Example</h3>

      <asp:placeholder id="MenuPlaceHolder"
        runat="server"/>

    </form>
  </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker