MenuItem 建構函式

定義

初始化 MenuItem 類別的新執行個體。

多載

MenuItem()

不以功能表文字或值,初始化 MenuItem 類別的新執行個體。

MenuItem(String)

使用指定的功能表文字,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String)

使用指定的功能表文字和值,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String)

使用指定的功能表文字、值和影像的 URL,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String, String)

使用指定的功能表文字、值、影像 URL 和巡覽 URL,初始化 MenuItem 類別的新執行個體。

MenuItem(String, String, String, String, String)

使用指定的功能表文字、值、影像 URL、巡覽 URL 和目標,初始化 MenuItem 類別的新執行個體。

MenuItem()

不以功能表文字或值,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem();
public MenuItem ();
Public Sub New ()

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // 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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }
  
  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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

<%@ 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)
    
    If Not IsPostBack Then
      
      ' 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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
    
  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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式來建立類別的新實例, MenuItem 而不需要功能表文字或值。

注意

使用此建構函式時,物件中的所有 MenuItem 屬性都會設定為其預設值。 建立物件之後,請務必視需要設定屬性。

這個建構函式通常會在動態填 Items 入 控制項的 Menu 集合或 ChildItems 物件的集合 MenuItem 時使用。

另請參閱

適用於

MenuItem(String)

使用指定的功能表文字,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text);
public MenuItem (string text);
new System.Web.UI.WebControls.MenuItem : string -> System.Web.UI.WebControls.MenuItem
Public Sub New (text As String)

參數

text
String

Menu 控制項中以文字顯示功能表項目。

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // Create the menu structure.

      // Create the root menu item.
      MenuItem homeMenuItem = new MenuItem("Home");

      // Create the submenu items.
      MenuItem musicSubMenuItem = new MenuItem("Music");
      MenuItem moviesSubMenuItem = new MenuItem("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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

<%@ 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)
  
    If Not IsPostBack Then
    
      ' Create the menu structure.

      ' Create the root menu item.
      Dim homeMenuItem As New MenuItem("Home")

      ' Create the submenu items.
      Dim musicSubMenuItem As New MenuItem("Music")
      Dim moviesSubMenuItem As New MenuItem("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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式,使用 參數指定的 text 功能表文字,建立 類別的新實例 MenuItem

下表顯示 類別實例 MenuItem 的初始屬性值。

屬性 初始值
Text text 參數的值。

這個建構函式通常會在動態填 Items 入 控制項的 Menu 集合或 ChildItems 物件的集合 MenuItem 時使用。

另請參閱

適用於

MenuItem(String, String)

使用指定的功能表文字和值,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text, System::String ^ value);
public MenuItem (string text, string value);
new System.Web.UI.WebControls.MenuItem : string * string -> System.Web.UI.WebControls.MenuItem
Public Sub New (text As String, value As String)

參數

text
String

Menu 控制項中以文字顯示功能表項目。

value
String

與此功能表項目有關的補充資料,例如,用來處理回傳事件的資料。

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // Create the menu structure.

      // Create the root menu item.
      MenuItem homeMenuItem = new MenuItem("Home", "Root");

      // Create the submenu items.
      MenuItem musicSubMenuItem = new MenuItem("Music", "Category 1");
      MenuItem moviesSubMenuItem = new MenuItem("Movies", "Category 2");

      // 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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

<%@ 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)
  
    If Not IsPostBack Then
    
      ' Create the menu structure.

      ' Create the root menu item.
      Dim homeMenuItem As New MenuItem("Home", "Root")

      ' Create the submenu items.
      Dim musicSubMenuItem As New MenuItem("Music", "Category 1")
      Dim moviesSubMenuItem As New MenuItem("Movies", "Category 2")

      ' 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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式,分別使用 和 value 參數指定的 text 功能表文字和值,建立 類別的新實例 MenuItem

下表所顯示的是 MenuItem 類別之執行個體的初始屬性值。

屬性 初始值
Text text 參數的值。
Value value 參數的值。

當動態填入 Items 控制項的集合或 ChildItems 物件的集合 Menu MenuItem 時,通常會使用這個建構函式。

另請參閱

適用於

MenuItem(String, String, String)

使用指定的功能表文字、值和影像的 URL,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text, System::String ^ value, System::String ^ imageUrl);
public MenuItem (string text, string value, string imageUrl);
new System.Web.UI.WebControls.MenuItem : string * string * string -> System.Web.UI.WebControls.MenuItem
Public Sub New (text As String, value As String, imageUrl As String)

參數

text
String

Menu 控制項中以文字顯示功能表項目。

value
String

與此功能表項目有關的補充資料,例如,用來處理回傳事件的資料。

imageUrl
String

顯示在功能表項目文字旁的影像之 URL。

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // Create the menu structure.

      // Create the root menu item.
      MenuItem homeMenuItem = new MenuItem("Home", "Root", 
        @"Images\Home.jpg");

      // Create the submenu items.
      MenuItem musicSubMenuItem = new MenuItem("Music", "Category 1", 
        @"Images\Music.jpg");
      MenuItem moviesSubMenuItem = new MenuItem("Movies", "Category 2", 
        @"Images\Movies.jpg");

      // 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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

<%@ 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)
  
    If Not IsPostBack Then
    
      ' Create the menu structure.

      ' Create the root menu item.
      Dim homeMenuItem As New MenuItem("Home", "Root", _
        "Images\Home.jpg")

      ' Create the submenu items.
      Dim musicSubMenuItem As New MenuItem("Music", "Category 1", _
        "Images\Music.jpg")
      Dim moviesSubMenuItem As New MenuItem("Movies", "Category 2", _
        "Images\Movies.jpg")

      ' 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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式可分別使用 、 valueimageUrl 參數所 text 指定的功能表文字、值和影像 URL,建立 類別的新實例 MenuItem

下表所顯示的是 MenuItem 類別之執行個體的初始屬性值。

屬性 初始值
Text text 參數的值。
Value value 參數的值。
ImageUrl imageUrl 參數的值。

當動態填入 Items 控制項的集合或 ChildItems 物件的集合 Menu MenuItem 時,通常會使用這個建構函式。

另請參閱

適用於

MenuItem(String, String, String, String)

使用指定的功能表文字、值、影像 URL 和巡覽 URL,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text, System::String ^ value, System::String ^ imageUrl, System::String ^ navigateUrl);
public MenuItem (string text, string value, string imageUrl, string navigateUrl);
new System.Web.UI.WebControls.MenuItem : string * string * string * string -> System.Web.UI.WebControls.MenuItem
Public Sub New (text As String, value As String, imageUrl As String, navigateUrl As String)

參數

text
String

Menu 控制項中以文字顯示功能表項目。

value
String

與此功能表項目有關的補充資料,例如,用來處理回傳事件的資料。

imageUrl
String

顯示在功能表項目文字旁的影像之 URL。

navigateUrl
String

在按一下功能表項目時所要連結的 URL。

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // Create the menu structure.

      // Create the root menu item.
      MenuItem homeMenuItem = new MenuItem("Home", "Root",
        @"Images\Home.jpg", "Home.aspx");

      // Create the submenu items.
      MenuItem musicSubMenuItem = new MenuItem("Music", "Category 1",
        @"Images\Music.jpg", "Music.aspx");
      MenuItem moviesSubMenuItem = new MenuItem("Movies", "Category 2",
        @"Images\Movies.jpg", "Movies.aspx");

      // 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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

<%@ 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)
  
    If Not IsPostBack Then
    
      ' Create the menu structure.

      ' Create the root menu item.
      Dim homeMenuItem As New MenuItem("Home", "Root", _
        "Images\Home.jpg", "Home.aspx")

      ' Create the submenu items.
      Dim musicSubMenuItem As New MenuItem("Music", "Category 1", _
        "Images\Music.jpg", "Music.aspx")
      Dim moviesSubMenuItem As New MenuItem("Movies", "Category 2", _
        "Images\Movies.jpg", "Movies.aspx")

      ' 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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式可分別使用 、 value imageUrl 、 和 navigateUrl 參數所 text 指定的功能表文字、值、影像 URL 和流覽 URL,建立 類別的新實例 MenuItem

下表所顯示的是 MenuItem 類別之執行個體的初始屬性值。

屬性 初始值
Text text 參數的值。
Value value 參數的值。
ImageUrl imageUrl 參數的值。
NavigateUrl navigateUrl 參數的值。

當動態填入 Items 控制項的集合或 ChildItems 物件的集合 Menu MenuItem 時,通常會使用這個建構函式。

另請參閱

適用於

MenuItem(String, String, String, String, String)

使用指定的功能表文字、值、影像 URL、巡覽 URL 和目標,初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text, System::String ^ value, System::String ^ imageUrl, System::String ^ navigateUrl, System::String ^ target);
public MenuItem (string text, string value, string imageUrl, string navigateUrl, string target);
new System.Web.UI.WebControls.MenuItem : string * string * string * string * string -> System.Web.UI.WebControls.MenuItem
Public Sub New (text As String, value As String, imageUrl As String, navigateUrl As String, target As String)

參數

text
String

Menu 控制項中以文字顯示功能表項目。

value
String

與此功能表項目有關的補充資料,例如,用來處理回傳事件的資料。

imageUrl
String

顯示在功能表項目文字旁的影像之 URL。

navigateUrl
String

在按一下功能表項目時所要連結的 URL。

target
String

按下功能表項目後,要顯示連結至功能表項目之 Web 網頁內容的目標視窗或框架。

範例

下列範例示範如何使用這個建構函式來建立 類別的新實例 MenuItem 。 然後,物件 MenuItem 會用來動態填入 控制項中的 Menu 功能表項目。


<%@ 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)
  {
    if (!IsPostBack)
    {
      // Create the menu structure.

      // Create the root menu item.
      MenuItem homeMenuItem = new MenuItem("Home", "Root",
        @"Images\Home.jpg", "Home.aspx", "_self");

      // Create the submenu items.
      MenuItem musicSubMenuItem = new MenuItem("Music", "Category 1",
        @"Images\Music.jpg", "Music.aspx", "_blank");
      MenuItem moviesSubMenuItem = new MenuItem("Movies", "Category 2",
        @"Images\Movies.jpg", "Movies.aspx", "_blank");

      // 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 Items collection 
      // of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem);
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        runat="server"/>

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

<%@ 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)
  
    If Not IsPostBack Then
    
      ' Create the menu structure.

      ' Create the root menu item.
      Dim homeMenuItem As New MenuItem("Home", "Root", _
        "Images\Home.jpg", "Home.aspx", "_self")

      ' Create the submenu items.
      Dim musicSubMenuItem As New MenuItem("Music", "Category 1", _
        "Images\Music.jpg", "Music.aspx", "_blank")
      Dim moviesSubMenuItem As New MenuItem("Movies", "Category 2", _
        "Images\Movies.jpg", "Movies.aspx", "_blank")

      ' 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 Items collection 
      ' of the Menu control.
      NavigationMenu.Items.Add(homeMenuItem)
      
    End If
  
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItem Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItem Constructor Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        runat="server"/>

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

備註

使用此建構函式可分別使用 、 valueimageUrl navigateUrl 、 和 target 參數指定的 text 功能表文字、值、影像 URL、流覽 URL 和目標,建立 類別的新實例 MenuItem

下表所顯示的是 MenuItem 類別之執行個體的初始屬性值。

屬性 初始值
Text text 參數的值。
Value value 參數的值。
ImageUrl imageUrl 參數的值。
NavigateUrl navigateUrl 參數的值。
Target target 參數的值。

當動態填入 Items 控制項的集合或 ChildItems 物件的集合 Menu MenuItem 時,通常會使用這個建構函式。

另請參閱

適用於