TreeNodeBindingCollection 類別

定義

代表 TreeNodeBinding 控制項中 TreeView 物件的集合。Represents a collection of TreeNodeBinding objects in the TreeView control. 此類別無法獲得繼承。This class cannot be inherited.

public ref class TreeNodeBindingCollection sealed : System::Web::UI::StateManagedCollection
public sealed class TreeNodeBindingCollection : System.Web.UI.StateManagedCollection
type TreeNodeBindingCollection = class
    inherit StateManagedCollection
Public NotInheritable Class TreeNodeBindingCollection
Inherits StateManagedCollection
繼承
TreeNodeBindingCollection

範例

本節包含兩個程式碼範例。This section contains two code examples. 第一個程式碼範例示範如何以宣告 TreeNodeBindingCollection 方式填入物件。The first code example demonstrates how to populate a TreeNodeBindingCollection object declaratively. 第二個程式碼範例示範如何以程式設計 TreeNodeBindingCollection 方式填入物件。The second code example demonstrates how to populate a TreeNodeBindingCollection object programmatically.

下列程式碼範例示範如何以宣告 TreeNodeBindingCollection 方式填入物件。The following code example demonstrates how to populate a TreeNodeBindingCollection object declaratively. 為了讓此範例正確運作,您必須將位於此區段結尾的 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView XML Data Binding Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView XML Data Binding 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView XML Data Binding Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView XML Data Binding 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>

下列程式碼範例示範如何以程式設計 TreeNodeBindingCollection 方式填入物件。The following code example demonstrates how to populate a TreeNodeBindingCollection object programmatically. 為了讓此範例正確運作,您必須將位於本節結尾的範例 XML 資料複製到名為 Book.xml 的檔案。For this example to work correctly, you must copy the sample 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)
  {

    // Create a new TreeView control.
    TreeView NewTree = new TreeView();

    // Set the properties of the TreeView control.
    NewTree.ID = "BookTreeView";
    NewTree.DataSourceID = "BookXmlDataSource";

    // Create the tree node binding relationship.

    // Create the root node binding.
    TreeNodeBinding RootBinding = new TreeNodeBinding();
    RootBinding.DataMember = "Book";
    RootBinding.TextField = "Title";

    // Create the parent node binding.
    TreeNodeBinding ParentBinding = new TreeNodeBinding();
    ParentBinding.DataMember = "Chapter";
    ParentBinding.TextField = "Heading";

    // Create the leaf node binding.
    TreeNodeBinding LeafBinding = new TreeNodeBinding();
    LeafBinding.DataMember = "Section";
    LeafBinding.TextField = "Heading";

    // Add bindings to the DataBindings collection.
    NewTree.DataBindings.Add(RootBinding);
    NewTree.DataBindings.Add(ParentBinding); 
    NewTree.DataBindings.Add(LeafBinding);

    // Manually register the event handler for the SelectedNodeChanged event.
    NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);

    // Add the TreeView control to the Controls collection of the PlaceHolder control.
    ControlPlaceHolder.Controls.Add(NewTree);

  }

  void Node_Change(Object sender, EventArgs e)
  {

    // Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
    TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");

    // Display the selected node.
    Message.Text = "You selected: " + LocalTree.SelectedNode.Text;

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView Constructor Example</h3>
      
      <asp:PlaceHolder id="ControlPlaceHolder" runat="server">
      </asp:PlaceHolder>
   
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
      
      <br /><br />
      
      <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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a new TreeView control.
    Dim NewTree As New TreeView

    ' Set the properties of the TreeView control.
    NewTree.ID = "BookTreeView"
    NewTree.DataSourceID = "BookXmlDataSource"

    ' Create the tree node binding relationship.

    ' Create the root node binding.
    Dim RootBinding As New TreeNodeBinding
    RootBinding.DataMember = "Book"
    RootBinding.TextField = "Title"

    ' Create the parent node binding.
    Dim ParentBinding As New TreeNodeBinding
    ParentBinding.DataMember = "Chapter"
    ParentBinding.TextField = "Heading"

    ' Create the leaf node binding.
    Dim LeafBinding As New TreeNodeBinding
    LeafBinding.DataMember = "Section"
    LeafBinding.TextField = "Heading"

    ' Add bindings to the DataBindings collection.
    NewTree.DataBindings.Add(RootBinding)
    NewTree.DataBindings.Add(ParentBinding)
    NewTree.DataBindings.Add(LeafBinding)

    ' Manually register the event handler for the SelectedNodeChanged event.
    AddHandler NewTree.SelectedNodeChanged, AddressOf Node_Change

    ' Add the TreeView control to the Controls collection of the PlaceHolder control.
    ControlPlaceHolder.Controls.Add(NewTree)

  End Sub

  Sub Node_Change(ByVal sender As Object, ByVal e As EventArgs)

    ' Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
    Dim LocalTree As TreeView = CType(ControlPlaceHolder.FindControl("BookTreeView"), TreeView)

    ' Display the selected node.
    Message.Text = "You selected: " & LocalTree.SelectedNode.Text

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView Constructor Example</h3>
      
      <asp:PlaceHolder id="ControlPlaceHolder" runat="server">
      </asp:PlaceHolder>
   
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
      
      <br /><br />
      
      <asp:Label id="Message" runat="server"/>
    
    </form>
  </body>
</html>

以下是上述程式碼範例的 XML 資料。The following is the XML data for the preceding code examples.

<Book Title="Book Title">  
    <Chapter Heading="Chapter 1">  
       <Section Heading="Section 1">  
       </Section>  
       <Section Heading="Section 2">  
       </Section>  
    </Chapter>  
    <Chapter Heading="Chapter 2">  
        <Section Heading="Section 1">  
        </Section>  
    </Chapter>  
</Book>  

備註

TreeNodeBindingCollection類別是用來儲存和管理 TreeNodeBinding 控制項中的物件集合 TreeViewThe TreeNodeBindingCollection class is used to store and manage a collection of TreeNodeBinding objects in the TreeView control. TreeView控制項使用類別做 TreeNodeBindingCollection 為其 DataBindings 屬性。The TreeView control uses the TreeNodeBindingCollection class for its DataBindings property.

DataBindings屬性包含 TreeNodeBinding 物件,這些物件會定義資料項目與其系結的節點之間的關聯性。The DataBindings property contains TreeNodeBinding objects that define the relationship between a data item and the node that it is binding to. 當系結至每個資料項目包含多個屬性的資料來源時(例如具有數個屬性的 XML 元素),節點預設會顯示資料項目的方法所傳回的值 ToStringWhen binding to a data source where each data item contains multiple properties (such as an XML element with several attributes), a node displays the value that is returned by the ToString method of the data item, by default. 在 XML 專案的案例中,節點會顯示元素名稱,這會顯示樹狀結構的基礎結構,但不會非常有用。In the case of an XML element, the node displays the element name, which shows the underlying structure of the tree but is not very useful otherwise. 您可以藉由指定樹狀節點系結,將節點系結至特定的資料項目屬性。You can bind a node to a specific data item property by specifying tree node bindings. 雖然可透過程式設計 DataBindings 方式填入集合,但通常會以宣告方式設定。Although the DataBindings collection can be programmatically populated, it is usually set declaratively.

以宣告方式設定樹狀節點系結:To set the tree node bindings declaratively:

  1. <DataBindings> 控制項的開頭和結束記號之間,嵌套開頭和結尾 TreeView 標記。Nest opening and closing <DataBindings> tags between the opening and closing tags of the TreeView control.

  2. <asp:TreeNodeBinding> <DataBindings> 您要指定的每個樹狀節點系結的開頭和結束記號之間放置元素。Place <asp:TreeNodeBinding> elements between the opening and closing <DataBindings> tags for each tree node binding that you want to specify.

您可以藉 TreeNodeBindingCollection 由加入和移除物件,以程式設計方式管理 TreeNodeBindingYou can programmatically manage a TreeNodeBindingCollection by adding and removing TreeNodeBinding objects. 若要將 TreeNodeBinding 物件加入至集合,請使用 AddInsert 方法。To add a TreeNodeBinding object to the collection, use the Add or Insert method. 若要從集合中移除節點,請使用 RemoveRemoveAtStateManagedCollection.Clear 方法。To remove nodes from the collection, use the Remove, RemoveAt, or StateManagedCollection.Clear method.

TreeNodeBindingCollection類別支援數種方式來存取集合中的專案:The TreeNodeBindingCollection class supports several ways to access the items in the collection:

屬性

Count

取得 StateManagedCollection 集合中所包含的項目數。Gets the number of elements contained in the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
Item[Int32]

取得或設定位於 TreeNodeBinding 物件中所指定索引處的 TreeNodeBindingCollection 物件。Gets or sets the TreeNodeBinding object at the specified index in the TreeNodeBindingCollection object.

方法

Add(TreeNodeBinding)

將指定的 TreeNodeBinding 物件附加至 TreeNodeBindingCollection 物件的結尾。Appends the specified TreeNodeBinding object to the end of the TreeNodeBindingCollection object.

Clear()

將所有項目從 StateManagedCollection 集合中移除。Removes all items from the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
Contains(TreeNodeBinding)

判斷指定的 TreeNodeBinding 物件是否在集合中。Determines whether the specified TreeNodeBinding object is in the collection.

CopyTo(Array, Int32)

從特定的陣列索引開始,將 StateManagedCollection 集合的項目複製到陣列。Copies the elements of the StateManagedCollection collection to an array, starting at a particular array index.

(繼承來源 StateManagedCollection)
CopyTo(TreeNodeBinding[], Int32)

從目標陣列中的指定索引開始,從 TreeNodeBindingCollection 物件中,將所有項目複製至相容的一維陣列 TreeNodeBinding 物件。Copies all the items from the TreeNodeBindingCollection object to a compatible one-dimensional array of TreeNodeBinding objects, starting at the specified index in the target array.

CreateKnownType(Int32)

在衍生類別中覆寫時,建立實作 IStateManager 之類別的執行個體。When overridden in a derived class, creates an instance of a class that implements IStateManager. 根據 GetKnownTypes() 方法所傳回之集合指定的成員,建立物件的類型。The type of object created is based on the specified member of the collection returned by the GetKnownTypes() method.

(繼承來源 StateManagedCollection)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回逐一查看 StateManagedCollection 集合的列舉值。Returns an iterator that iterates through the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetKnownTypes()

在衍生類別中覆寫時,取得 StateManagedCollection 集合可以包含之 IStateManager 類型的陣列。When overridden in a derived class, gets an array of IStateManager types that the StateManagedCollection collection can contain.

(繼承來源 StateManagedCollection)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(TreeNodeBinding)

判斷集合中指定之 TreeNodeBinding 物件的索引。Determines the index of the specified TreeNodeBinding object in the collection.

Insert(Int32, TreeNodeBinding)

將指定的 TreeNodeBinding 物件插入 TreeNodeBindingCollection 物件的指定索引位置。Inserts the specified TreeNodeBinding object into the TreeNodeBindingCollection object at the specified index location.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnClear()

在衍生類別中覆寫時,在 Clear() 方法移除集合的所有項目之前,執行額外工作。When overridden in a derived class, performs additional work before the Clear() method removes all items from the collection.

(繼承來源 StateManagedCollection)
OnClearComplete()

在衍生類別中覆寫時,在 Clear() 方法完成移除集合的所有項目之後,執行額外工作。When overridden in a derived class, performs additional work after the Clear() method finishes removing all items from the collection.

(繼承來源 StateManagedCollection)
OnInsert(Int32, Object)

在衍生類別中覆寫時,在 IList.Insert(Int32, Object)IList.Add(Object) 方法將項目加入集合之前,執行額外工作。When overridden in a derived class, performs additional work before the IList.Insert(Int32, Object) or IList.Add(Object) method adds an item to the collection.

(繼承來源 StateManagedCollection)
OnInsertComplete(Int32, Object)

在衍生類別中覆寫時,在 IList.Insert(Int32, Object)IList.Add(Object) 方法將項目加入集合之後,執行額外工作。When overridden in a derived class, performs additional work after the IList.Insert(Int32, Object) or IList.Add(Object) method adds an item to the collection.

(繼承來源 StateManagedCollection)
OnRemove(Int32, Object)

在衍生類別中覆寫時,在 IList.Remove(Object)IList.RemoveAt(Int32) 方法從集合中移除指定的項目之前,執行額外工作。When overridden in a derived class, performs additional work before the IList.Remove(Object) or IList.RemoveAt(Int32) method removes the specified item from the collection.

(繼承來源 StateManagedCollection)
OnRemoveComplete(Int32, Object)

在衍生類別中覆寫時,在 IList.Remove(Object)IList.RemoveAt(Int32) 方法從集合中移除指定的項目之後,執行額外工作。When overridden in a derived class, performs additional work after the IList.Remove(Object) or IList.RemoveAt(Int32) method removes the specified item from the collection.

(繼承來源 StateManagedCollection)
OnValidate(Object)

在衍生類別中覆寫時,驗證 StateManagedCollection 集合的項目。When overridden in a derived class, validates an element of the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
Remove(TreeNodeBinding)

TreeNodeBinding 物件移除指定的 TreeNodeBindingCollection 物件。Removes the specified TreeNodeBinding object from the TreeNodeBindingCollection object.

RemoveAt(Int32)

TreeNodeBinding 物件的指定索引位置移除 TreeNodeBindingCollection 物件。Removes the TreeNodeBinding object at the specified index location from the TreeNodeBindingCollection object.

SetDirty()

強制整個 StateManagedCollection 集合序列化至檢視狀態。Forces the entire StateManagedCollection collection to be serialized into view state.

(繼承來源 StateManagedCollection)
SetDirtyObject(Object)

在衍生類別中覆寫時,指示集合中所包含的 object,將其完整狀態 (而不只是變更資訊) 記錄至檢視狀態。When overridden in a derived class, instructs an object contained by the collection to record its entire state to view state, rather than recording only change information.

(繼承來源 StateManagedCollection)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.Count

取得 StateManagedCollection 集合中所包含的項目數。Gets the number of elements contained in the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
ICollection.IsSynchronized

取得值,表示 StateManagedCollection 集合是否為同步 (安全執行緒)。Gets a value indicating whether the StateManagedCollection collection is synchronized (thread safe). 在所有情況下,這個方法都會傳回 falseThis method returns false in all cases.

(繼承來源 StateManagedCollection)
ICollection.SyncRoot

取得物件,可用來同步處理對 StateManagedCollection 集合的存取。Gets an object that can be used to synchronize access to the StateManagedCollection collection. 在所有情況下,這個方法都會傳回 nullThis method returns null in all cases.

(繼承來源 StateManagedCollection)
IEnumerable.GetEnumerator()

傳回逐一查看 StateManagedCollection 集合的列舉值。Returns an iterator that iterates through the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
IList.Add(Object)

將項目加入 StateManagedCollection 集合。Adds an item to the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
IList.Clear()

將所有項目從 StateManagedCollection 集合中移除。Removes all items from the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
IList.Contains(Object)

判斷 StateManagedCollection 集合是否包含特定值。Determines whether the StateManagedCollection collection contains a specific value.

(繼承來源 StateManagedCollection)
IList.IndexOf(Object)

判斷 StateManagedCollection 集合中指定之項目的索引。Determines the index of a specified item in the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
IList.Insert(Int32, Object)

將項目插入位於指定索引處的 StateManagedCollection 集合中。Inserts an item into the StateManagedCollection collection at the specified index.

(繼承來源 StateManagedCollection)
IList.IsFixedSize

取得值,表示 StateManagedCollection 集合是否具有固定大小。Gets a value indicating whether the StateManagedCollection collection has a fixed size. 在所有情況下,這個方法都會傳回 falseThis method returns false in all cases.

(繼承來源 StateManagedCollection)
IList.IsReadOnly

取得值,表示 StateManagedCollection 集合是否為唯讀。Gets a value indicating whether the StateManagedCollection collection is read-only.

(繼承來源 StateManagedCollection)
IList.Item[Int32]

取得指定索引處的 IStateManager 項目。Gets the IStateManager element at the specified index.

(繼承來源 StateManagedCollection)
IList.Remove(Object)

StateManagedCollection 集合中移除指定物件的第一個符合項目。Removes the first occurrence of the specified object from the StateManagedCollection collection.

(繼承來源 StateManagedCollection)
IList.RemoveAt(Int32)

移除指定索引處的 IStateManager 項目。Removes the IStateManager element at the specified index.

(繼承來源 StateManagedCollection)
IStateManager.IsTrackingViewState

取得值,表示 StateManagedCollection 集合是否正在儲存變更至檢視狀態。Gets a value indicating whether the StateManagedCollection collection is saving changes to its view state.

(繼承來源 StateManagedCollection)
IStateManager.LoadViewState(Object)

還原先前儲存之 StateManagedCollection 集合和內含 IStateManager 項目的檢視狀態。Restores the previously saved view state of the StateManagedCollection collection and the IStateManager items it contains.

(繼承來源 StateManagedCollection)
IStateManager.SaveViewState()

儲存自頁面回傳至伺服器以來 StateManagedCollection 集合和每個內含 IStateManager 物件的變更。Saves the changes to the StateManagedCollection collection and each IStateManager object it contains since the time the page was posted back to the server.

(繼承來源 StateManagedCollection)
IStateManager.TrackViewState()

使得 StateManagedCollection 集合和每個內含 IStateManager 物件追蹤其檢視狀態的變更,以便跨相同頁面要求保存這些變更。Causes the StateManagedCollection collection and each of the IStateManager objects it contains to track changes to their view state so they can be persisted across requests for the same page.

(繼承來源 StateManagedCollection)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱