Прочитај на енглеском Уреди

Делите путем


MenuItemCollection.CopyTo Method

Definition

Copies the contents of the current MenuItemCollection object.

Overloads

CopyTo(Array, Int32)

Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

CopyTo(MenuItem[], Int32)

Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array.

CopyTo(Array, Int32)

Copies all the items from the MenuItemCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

C#
public void CopyTo(Array array, int index);

Parameters

array
Array

A zero-based Array that receives the copied items from the current MenuItemCollection.

index
Int32

The position in the target array at which to start receiving the copied content.

Implements

Exceptions

array is not an array of MenuItem objects.

Remarks

Use the CopyTo method to copy the contents of the current MenuItemCollection object into the specified zero-based System.Array. Items are copied starting at the specified index of the target array. With the System.Array, you can then use array syntax to access the items in the MenuItemCollection object.

As an alternative, you can also use the GetEnumerator method to create an enumerator that can be used to access the items in the collection.

See also

Applies to

.NET Framework 4.8.1 и друге верзије
Производ Верзије
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

CopyTo(MenuItem[], Int32)

Copies all the items from the MenuItemCollection object to a compatible one-dimensional array of MenuItem objects, starting at the specified index in the target array.

C#
public void CopyTo(System.Web.UI.WebControls.MenuItem[] array, int index);

Parameters

array
MenuItem[]

A zero-based array of MenuItem objects that receives the copied items from the current MenuItemCollection.

index
Int32

The position in the target array at which to start receiving the copied content.

Examples

The following code example demonstrates how to use the CopyTo method to copy the items in a MenuItemCollection object to an array of MenuItem objects.

ASP.NET (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)
  {

    // Display the submenu items of the Music
    // menu item. 

    // Retrieve the Music menu item.
    MenuItem musicMenuItem = NavigationMenu.FindItem(@"Home");

    // Declare an array of MenuItem objects.
         MenuItem[] musicItemArray = new MenuItem[musicMenuItem.ChildItems.Count];

    // Use the CopyTo method to copy the submenu items 
    // of the Music menu item into the array.
    musicMenuItem.ChildItems.CopyTo(musicItemArray, 0);
    
    // Display the menu items.
    Message.Text = "The submenu items of the Home menu item are: <br/><br/>";

    foreach (MenuItem item in musicItemArray)
    {

      Message.Text += item.Text + "<br />";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemCollection CopyTo Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemCollection CopyTo Example</h3>
    
      <asp:menu id="NavigationMenu"
        orientation="Vertical"
        target="_blank" 
        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>

Remarks

Use the CopyTo method to copy the contents of the current MenuItemCollection object into the specified zero-based array. Items are copied starting at the specified index of the target array. With the array, you can then use array syntax to access the items in the MenuItemCollection object.

As an alternative, you can also use the GetEnumerator method to create an enumerator that can be used to access the items in the collection.

See also

Applies to

.NET Framework 4.8.1 и друге верзије
Производ Верзије
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1