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虽然集合可以以编程方式填充,但通常以声明方式设置该集合。
若要以声明方式设置树节点绑定,
在控件的TreeView开始标记和结束标记之间嵌套打开和
<DataBindings>
结束标记。将元素放在
<asp:TreeNodeBinding>
要指定的每个树节点绑定的开始标记和结束<DataBindings>
标记之间。
可以通过添加和删除TreeNodeBinding对象以编程方式管理对象TreeNodeBindingCollection。 若要将 TreeNodeBinding 对象添加到集合,请使用 Add 或 Insert 方法。 若要从集合中删除节点,请使用Remove或RemoveAtStateManagedCollection.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。 |