MenuItem.ChildItems プロパティ
アセンブリ: System.Web (system.web.dll 内)

現在のメニュー項目のサブメニュー項目を格納している MenuItemCollection。既定値は null 参照 (Visual Basic では Nothing) で、このメニュー項目にサブメニュー項目がないことを示します。

現在のメニュー項目にサブメニュー項目がある場合は、ChildItems プロパティ (コレクション) を使用してサブメニュー項目にアクセスします。このコレクションには、次のレベルのメニュー項目だけが含まれます。メニュー ツリーのさらに下のメニュー項目にアクセスするには、後続のメニュー項目の ChildItems プロパティを使用します。ChildItems プロパティが null 参照 (Visual Basic では Nothing) の場合は、現在のメニューにサブメニュー項目はありません。
ChildItems プロパティを使用して、現在のメニュー項目のサブメニュー項目をプログラムによって管理することもできます。コレクションの MenuItem オブジェクトを追加、挿入、削除、取得、および変更できます。このコレクションに加えられた更新は、ページが次に更新されるときに Menu コントロールに自動的に反映されます。

ChildItems プロパティを使用して、Menu コントロールのメニュー項目を走査する方法のコード例を次に示します。
<%@ Page Language="VB" %> <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> <body> <form 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="C#" %> <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> <body> <form 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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- MenuItem.ChildItems プロパティのページへのリンク