다음을 통해 공유


MenuItemCollection.IndexOf(MenuItem) 메서드

정의

컬렉션에서 지정된 MenuItem 개체의 인덱스를 확인합니다.

public:
 int IndexOf(System::Web::UI::WebControls::MenuItem ^ value);
public int IndexOf (System.Web.UI.WebControls.MenuItem value);
member this.IndexOf : System.Web.UI.WebControls.MenuItem -> int
Public Function IndexOf (value As MenuItem) As Integer

매개 변수

value
MenuItem

찾을 MenuItem입니다.

반환

Int32

현재 MenuItemCollection에서 value가 처음 나타나는 인덱스(0부터 시작)입니다. 값이 컬렉션에 없는 경우 -1입니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 IndexOf 의 인덱스를 확인 하는 방법을 MenuItem 내에서 개체를 MenuItemCollection 개체입니다.


<%@ 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 NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
  {

    // Retrieve the parent menu item of the selected menu item.
    MenuItem parent = e.Item.Parent;

    // Determine whether the selected menu item is a 
    // root menu item. Root menu items are stored in
    // a Menu control's Items collection. Other menu
    // items are stored in the parent menu item's
    // ChildItems collection.
    if (parent != null)
    {
      Message.Text = "The " + e.Item.Text + 
        " menu item has an index of " +
        parent.ChildItems.IndexOf(e.Item).ToString() +
        " in the parent menu item's ChildItems collection.";
    }
    else
    {
      Message.Text = "The " + e.Item.Text +
        " menu item has an index of " +
        NavigationMenu.Items.IndexOf(e.Item).ToString() +
        " in the Menu control's Items collection.";
    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection IndexOf Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection IndexOf Example</h3>
    
      Select an item from the menu.
      <br/><br/>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank"
        onmenuitemclick="NavigationMenu_MenuItemClick"  
        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 NavigationMenu_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)

    ' Retrieve the parent menu item of the selected menu item.
    Dim parent As MenuItem = e.Item.Parent

    ' Determine whether the selected menu item is a 
    ' root menu item. Root menu items are stored in
    ' a Menu control's Items collection. Other menu
    ' items are stored in the parent menu item's
    ' ChildItems collection.
    If parent IsNot Nothing Then
    
      Message.Text = "The " & e.Item.Text & _
        " menu item has an index of " & _
        parent.ChildItems.IndexOf(e.Item).ToString() & _
        " in the parent menu item's ChildItems collection."
    
    Else
    
      Message.Text = "The " & e.Item.Text & _
        " menu item has an index of " & _
        NavigationMenu.Items.IndexOf(e.Item).ToString() & _
        " in the Menu control's Items collection."
    
    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection IndexOf Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection IndexOf Example</h3>
    
      Select an item from the menu.
      <br/><br/>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank"
        onmenuitemclick="NavigationMenu_MenuItemClick"  
        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>

설명

사용 된 IndexOf 지정 된 인덱스를 확인 하는 방법 MenuItem 개체를 현재 MenuItemCollection 개체입니다. 메뉴 항목 컬렉션에 없는 경우 메서드는-1을 반환 합니다.

참고

지정한 메뉴 항목을 컬렉션에 여러 번 나타나면 첫 번째 발생의 인덱스가 반환 됩니다.

적용 대상

추가 정보