TreeNodeBindingCollection.RemoveAt(Int32) 方法
定义
从 TreeNodeBinding 对象中移除指定索引位置的 TreeNodeBindingCollection 对象。Removes the TreeNodeBinding object at the specified index location from the TreeNodeBindingCollection object.
public:
void RemoveAt(int index);
public void RemoveAt (int index);
member this.RemoveAt : int -> unit
Public Sub RemoveAt (index As Integer)
参数
- index
- Int32
要移除的 TreeNodeBinding 的从零开始的索引位置。The zero-based index location of the TreeNodeBinding to remove.
示例
下面的代码示例演示如何使用 RemoveAt 方法以 TreeNodeBinding 编程方式从对象中删除对象 TreeNodeBindingCollection 。The following code example demonstrates how to use the RemoveAt method to remove a TreeNodeBinding object programmatically from a TreeNodeBindingCollection object. 若要使此示例正常工作,必须将位于此部分末尾的 XML 数据复制到名为 Book.xml 的文件中。For this example to work correctly, you must copy the XML data that is located at the end of this section to a file called 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 RemoveAt method to remove the TreeNodeBinding object
// for the third-level nodes (index 2).
BookTreeView.DataBindings.RemoveAt(2);
// Create a new TreeNodeBinding object and set its properties.
TreeNodeBinding newBinding = new TreeNodeBinding();
newBinding.DataMember = "Section";
newBinding.TextField = "Subject";
// Use the Insert method to insert the TreeNodeBinding object
// into the DaaBindings collection at index 2.
BookTreeView.DataBindings.Insert(2, newBinding);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNodeBindingCollection Insert and RemoveAt Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNodeBindingCollection Insert and RemoveAt 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 RemoveAt method to remove the TreeNodeBinding object
' for the third-level nodes (index 2).
BookTreeView.DataBindings.RemoveAt(2)
' Create a new TreeNodeBinding object and set its properties.
Dim newBinding As TreeNodeBinding = New TreeNodeBinding
newBinding.DataMember = "Section"
newBinding.TextField = "Subject"
' Use the Insert method to insert the TreeNodeBinding object to
' the DataBindings collection at index 2.
BookTreeView.DataBindings.Insert(2, newBinding)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNodeBindingCollection Insert and RemoveAt Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNodeBindingCollection Insert and RemoveAt 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 数据。The following is the XML data for the preceding code example.
<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>
注解
使用 RemoveAt 方法 TreeNodeBinding index 从对象中移除位于处的对象 TreeNodeBindingCollection 。Use the RemoveAt method to remove the TreeNodeBinding object located at index from a TreeNodeBindingCollection object. 然后,在该节点之后的所有项上移以填充空白位置。All items that follow that node are then moved up to fill the vacant position. 已移动项的索引也会更新。The indexes of the moved items are also updated.
作为替代方法,您可以 TreeNodeBinding 通过使用方法删除特定的对象 Remove 。As an alternative, you can remove a specific TreeNodeBinding object by using the Remove method. 你还可以使用方法移除集合中的每个节点 Clear 。You can also remove every node in the collection by using the Clear method.