TreeNodeBindingCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 TreeNodeBinding 控制項中 TreeView 物件的集合。 此類別無法獲得繼承。
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 物件。 第二個程式碼範例示範如何以程式設計方式填入 TreeNodeBindingCollection 物件。
下列程式碼範例示範如何以宣告方式填入 TreeNodeBindingCollection 物件。 若要讓此範例正常運作,您必須將位於本節結尾的 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">
<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 物件。 若要讓此範例正常運作,您必須將位於本節結尾的範例 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)
{
// 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 資料。
<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 可用來儲存和管理 控制項中的 TreeView 物件集合 TreeNodeBinding 。 控制項 TreeView 會 TreeNodeBindingCollection 針對其 DataBindings 屬性使用 類別。
屬性 DataBindings 包含 TreeNodeBinding 物件,定義資料項目與其系結之節點之間的關聯性。 系結至資料來源時,每個資料項目都包含多個屬性 (,例如具有數個屬性的 XML 元素) ,節點預設會顯示資料項目方法所 ToString
傳回的值。 在 XML 元素的情況下,節點會顯示專案名稱,其中顯示樹狀結構的基礎結構,但不是非常有用的。 您可以藉由指定樹狀節點系結,將節點系結至特定的資料項目屬性。
DataBindings雖然集合可以程式設計方式填入,但通常是以宣告方式設定。
若要以宣告方式設定樹狀節點系結:
在控制項的開頭和結束記號之間巢狀開啟和
<DataBindings>
結束記號 TreeView 。將元素放在
<asp:TreeNodeBinding>
您要指定之每個樹狀節點系結的開頭和結尾<DataBindings>
標記之間。
您可以透過新增和移除 TreeNodeBinding 物件,以程式設計方式管理 TreeNodeBindingCollection 。 若要將 物件新增 TreeNodeBinding 至集合,請使用 Add 或 Insert 方法。 若要從集合中移除節點,請使用 Remove 、 RemoveAt 或 StateManagedCollection.Clear 方法。
類別 TreeNodeBindingCollection 支援數種方式來存取集合中的專案:
StateManagedCollection.GetEnumerator使用 方法來建立可用來逐一查看集合的列舉值。
屬性
Count |
取得 StateManagedCollection 集合中所包含的項目數。 (繼承來源 StateManagedCollection) |
Item[Int32] |
取得或設定位於 TreeNodeBinding 物件中所指定索引處的 TreeNodeBindingCollection 物件。 |
方法
明確介面實作
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |