MenuItem.ChildItems Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene un MenuItemCollection oggetto che contiene le voci del sottomenu della voce di menu corrente.
public:
property System::Web::UI::WebControls::MenuItemCollection ^ ChildItems { System::Web::UI::WebControls::MenuItemCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)]
public System.Web.UI.WebControls.MenuItemCollection ChildItems { get; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)>]
member this.ChildItems : System.Web.UI.WebControls.MenuItemCollection
Public ReadOnly Property ChildItems As MenuItemCollection
Valore della proprietà
Oggetto MenuItemCollection contenente le voci di sottomenu della voce di menu corrente. Il valore predefinito è null, che indica che questa voce di menu non contiene elementi di sottomenu.
- Attributi
Esempio
Nell'esempio seguente viene illustrato come utilizzare la ChildItems proprietà per attraversare le voci di menu in un Menu controllo .
<%@ 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 the Menu control contains any root nodes, perform a
// preorder traversal of the tree and display the text of
// each node.
if (NavigationMenu.Items.Count > 0)
{
// Iterate through the root menu items in the Items collection.
foreach (MenuItem item in NavigationMenu.Items)
{
// Display the menu items.
DisplayChildMenuText(item);
}
}
else
{
Message.Text = "The Menu control does not have any items.";
}
}
void DisplayChildMenuText(MenuItem item)
{
// Display the menu item's text value.
Message.Text += item.Text + "<br />";
// Iterate through the child menu items of the parent menu item
// passed into this method, and display their values.
foreach (MenuItem childItem in item.ChildItems)
{
// Recursively call the DisplayChildMenuText method to
// traverse the tree and display all child menu items.
DisplayChildMenuText(childItem);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Menu Items Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Menu Items Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
runat="server">
<items>
<asp:menuitem text="Home"
tooltip="Home">
<asp:menuitem text="Music"
tooltip="Music">
<asp:menuitem text="Classical"
tooltip="Classical"/>
<asp:menuitem text="Rock"
tooltip="Rock"/>
<asp:menuitem text="Jazz"
tooltip="Jazz"/>
</asp:menuitem>
<asp:menuitem text="Movies"
tooltip="Movies">
<asp:menuitem text="Action"
tooltip="Action"/>
<asp:menuitem text="Drama"
tooltip="Drama"/>
<asp:menuitem text="Musical"
tooltip="Musical"/>
</asp:menuitem>
</asp:menuitem>
</items>
</asp:menu>
<hr/>
<asp:label id="Message"
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 the Menu control contains any root nodes, perform a
' preorder traversal of the tree and display the text of
' each node.
If NavigationMenu.Items.Count > 0 Then
' Iterate through the root menu items in the Items collection.
Dim item As MenuItem
For Each item In NavigationMenu.Items
' Display the menu items.
DisplayChildMenuText(item)
Next
Else
Message.Text = "The Menu control does not have any items."
End If
End Sub
Sub DisplayChildMenuText(ByVal item As MenuItem)
' Display the menu item's text value.
Message.Text &= item.Text & "<br />"
' Iterate through the child menu items of the parent menu item
' passed into this method, and display their values.
Dim childItem As MenuItem
For Each childItem In item.ChildItems
' Recursively call the DisplayChildMenuText method to
' traverse the tree and display all child menu items.
DisplayChildMenuText(childItem)
Next
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Menu Items Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Menu Items Example</h3>
<asp:menu id="NavigationMenu"
staticdisplaylevels="2"
staticsubmenuindent="10"
orientation="Vertical"
runat="server">
<items>
<asp:menuitem text="Home"
tooltip="Home">
<asp:menuitem text="Music"
tooltip="Music">
<asp:menuitem text="Classical"
tooltip="Classical"/>
<asp:menuitem text="Rock"
tooltip="Rock"/>
<asp:menuitem text="Jazz"
tooltip="Jazz"/>
</asp:menuitem>
<asp:menuitem text="Movies"
tooltip="Movies">
<asp:menuitem text="Action"
tooltip="Action"/>
<asp:menuitem text="Drama"
tooltip="Drama"/>
<asp:menuitem text="Musical"
tooltip="Musical"/>
</asp:menuitem>
</asp:menuitem>
</items>
</asp:menu>
<hr/>
<asp:label id="Message"
runat="server"/>
</form>
</body>
</html>
Commenti
Utilizzare la ChildItems proprietà (raccolta) per accedere alle voci del sottomenu della voce di menu corrente, se presente. Questa raccolta contiene solo le voci di menu al livello successivo. Per accedere alle voci di menu più in basso nell'albero dei menu, utilizzare la ChildItems proprietà di una voce di menu successiva. Se la ChildItems proprietà è null, il menu corrente non dispone di alcun sottomenu.
La ChildItems proprietà può essere utilizzata anche per gestire a livello di codice le voci del sottomenu della voce di menu corrente. È possibile aggiungere, inserire, rimuovere, recuperare e modificare MenuItem oggetti dalla raccolta. Tutti gli aggiornamenti alla raccolta verranno automaticamente visualizzati nel Menu controllo alla successiva aggiornamento della pagina.