TreeNodeBindingCollection.Remove(TreeNodeBinding) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 TreeNodeBinding 物件移除指定的 TreeNodeBindingCollection 物件。
public:
void Remove(System::Web::UI::WebControls::TreeNodeBinding ^ binding);
public void Remove (System.Web.UI.WebControls.TreeNodeBinding binding);
member this.Remove : System.Web.UI.WebControls.TreeNodeBinding -> unit
Public Sub Remove (binding As TreeNodeBinding)
參數
- binding
- TreeNodeBinding
要移除的 TreeNodeBinding。
範例
下列程式碼範例示範如何使用 Remove 方法,以程式設計方式從 TreeNodeBindingCollection 物件中移除 TreeNodeBinding 物件。 若要讓此範例正常運作,您必須將位於本節結尾的 XML 資料複製到名為 Book.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)
{
// Use the Remove method to remove the TreeNodeBinding object
// for the third-level nodes (index 2).
TreeNodeBinding oldBinding = BookTreeView.DataBindings[2];
BookTreeView.DataBindings.Remove(oldBinding);
// Create a new TreeNodeBinding object and set its properties.
TreeNodeBinding newBinding = new TreeNodeBinding();
newBinding.DataMember = "Section";
newBinding.TextField = "Subject";
// Use the Add method to add the TreeNodeBinding object to the
// DataBindings collection.
BookTreeView.DataBindings.Add(newBinding);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNodeBindingCollection Add and Remove Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNodeBindingCollection Add and Remove Example</h3>
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
<asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
<asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
</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)
' Use the Remove method to remove the TreeNodeBinding object
' for the third-level nodes (index 2).
Dim oldBinding As TreeNodeBinding = BookTreeView.DataBindings(2)
BookTreeView.DataBindings.Remove(oldBinding)
' Create a new TreeNodeBinding object and set its properties.
Dim newBinding As TreeNodeBinding = New TreeNodeBinding
newBinding.DataMember = "Section"
newBinding.TextField = "Subject"
' Use the Add method to add the TreeNodeBinding object to the
' DataBindings collection.
BookTreeView.DataBindings.Add(newBinding)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNodeBindingCollection Add and Remove Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNodeBindingCollection Add and Remove Example</h3>
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
<asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
<asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
</form>
</body>
</html>
以下是上述程式碼範例的 XML 資料。
<Book Title="Book Title">
<Chapter Heading="Chapter 1">
<Section Heading="Section 1"
Subject="Subject 1">
</Section>
<Section Heading="Section 2"
Subject="Subject 1">
</Section>
</Chapter>
<Chapter Heading="Chapter 2">
<Section Heading="Section 1"
Subject="Subject 1">
</Section>
</Chapter>
</Book>
備註
Remove使用 方法可從集合中移除指定的 TreeNodeBinding 物件。 接著,該節點之後的所有專案都會向上移動,以填滿空的位置。 移動專案的索引也會更新。
或者,您可以使用 方法來移除特定索引 RemoveAt 上的節點。 您也可以使用 Clear 方法移除集合中的每個節點。