共用方式為


MenuItemBindingCollection.Remove(MenuItemBinding) 方法

定義

從集合中移除指定的 MenuItemBinding 物件。

public:
 void Remove(System::Web::UI::WebControls::MenuItemBinding ^ binding);
public void Remove (System.Web.UI.WebControls.MenuItemBinding binding);
member this.Remove : System.Web.UI.WebControls.MenuItemBinding -> unit
Public Sub Remove (binding As MenuItemBinding)

參數

binding
MenuItemBinding

要從集合中移除的 MenuItemBinding

範例

下列程式碼範例示範如何使用 Remove 方法從集合中移除 MenuItemBinding 物件。 若要讓此範例正常運作,您必須將下面的範例 XML 資料複製到名為 Map.xml 的檔案。


<%@ 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 item bindings for the Menu control.
      MenuItemBinding binding;
      
      binding = CreateMenuItemBinding("MapHomeNode", 0, "title", "url");
      NavigationMenu.DataBindings.Add(binding);

      binding = CreateMenuItemBinding("MapNode", 1, "title", "url");
      NavigationMenu.DataBindings.Add(binding);

      binding = CreateMenuItemBinding("MapNode", 2, "title", "url");
      NavigationMenu.DataBindings.Add(binding);
        
      // Use the Remove method to remove the last MenuItemBinding 
      // object.
        NavigationMenu.DataBindings.Remove(binding);

    }
  }

  // This is a helper method to create a MenuItemBinding 
  // object from the specified parameters.
  MenuItemBinding CreateMenuItemBinding(String dataMember, int depth, String textField, String navigateUrlField)
  {
    // Create a new MenuItemBinding object.
    MenuItemBinding binding = new MenuItemBinding();

    // Set the properties of the MenuItemBinding object.
    binding.DataMember = dataMember;
    binding.Depth = depth;
    binding.TextField = textField;
    binding.NavigateUrlField = navigateUrlField;

    return binding;
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBindingCollection Remove Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemBindingCollection Remove Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        runat="server">
        
        <DataBindings>
          <asp:menuitembinding datamember="MapHomeNode" 
            depth="0"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="ExtraMapNode" 
            depth="3"
            textfield="title" 
            navigateurlfield="url"/>
        </DataBindings>
                
      </asp:menu>
      
      <asp:xmldatasource id="MenuSource"
        datafile="Map.xml"
        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 item bindings for the Menu control.
            Dim binding As MenuItemBinding
      
            binding = CreateMenuItemBinding("MapHomeNode", 0, "title", "url")
            NavigationMenu.DataBindings.Add(binding)

            binding = CreateMenuItemBinding("MapNode", 1, "title", "url")
            NavigationMenu.DataBindings.Add(binding)

            binding = CreateMenuItemBinding("MapNode", 2, "title", "url")
            NavigationMenu.DataBindings.Add(binding)
            
            ' Use the Remove method to remove the MenuItemBinding 
            ' object.
            NavigationMenu.DataBindings.Remove(binding)
   
        End If
    
    End Sub

    ' This is a helper method to create a MenuItemBinding 
    ' object from the specified parameters.
    Function CreateMenuItemBinding(ByVal dataMember As String, ByVal depth As Integer, ByVal textField As String, ByVal navigateUrlField As String) As MenuItemBinding
  
        ' Create a new MenuItemBinding object.
        Dim binding As New MenuItemBinding()

        ' Set the properties of the MenuItemBinding object.
        binding.DataMember = dataMember
        binding.Depth = depth
        binding.TextField = textField
        binding.NavigateUrlField = navigateUrlField

        Return binding
    
    End Function
    
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>MenuItemBindingCollection Remove Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>MenuItemBindingCollection Remove Example</h3>
    
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        runat="server">
        
        <DataBindings>
          <asp:menuitembinding datamember="MapHomeNode" 
            depth="0"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="1"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="MapNode" 
            depth="2"
            textfield="title" 
            navigateurlfield="url"/>
          <asp:menuitembinding datamember="ExtraMapNode" 
            depth="3"
            textfield="title" 
            navigateurlfield="url"/>
        </DataBindings>
                
      </asp:menu>
      
      <asp:xmldatasource id="MenuSource"
        datafile="Map.xml"
        runat="server"/>        

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

以下是上一個範例的網站地圖資料範例。

<MapHomeNode url="~\Home.aspx"

title="Home"

description="Home">

<MapNode url="~\Music.aspx"

title="Music"

description="Music">

<MapNode url="~\Classical.aspx"

title="Classical"

description="Classical"/>

<MapNode url="~\Rock.aspx"

title="Rock"

description="Rock"/>

<MapNode url="~\Jazz.aspx"

title="Jazz"

description="Jazz"/>

</MapNode>

<MapNode url="~\Movies.aspx"

title="Movies"

description="Movies">

<MapNode url="~\Action.aspx"

title="Action"

description="Action"/>

<MapNode url="~\Drama.aspx"

title="Drama"

description="Drama"/>

<MapNode url="~\Musical.aspx"

title="Musical"

description="Musical"/>

</MapNode>

</MapHomeNode>

備註

Remove使用 方法可從集合中移除指定的功能表項目系結。 然後,該功能表項目後面的所有專案都會向上移動,以填入空的位置。 移動專案的索引也會更新。

注意

集合可以包含 Null 值,以及重複 MenuItemBinding 的物件。

或者,您可以使用 方法,從集合 RemoveAt 中的特定索引移除功能表項目。 您也可以使用 Clear 方法,從集合中移除所有功能表項目。

注意

物件出現在集合中的順序 MenuItemBinding 不會影響這些物件如何套用至控制項中的 Menu 功能表項目。

適用於

另請參閱