HierarchicalDataBoundControl 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
充当所有 ASP.NET 版本 2.0 数据绑定控件的基类,这些控件以分层形式显示其数据。
public ref class HierarchicalDataBoundControl abstract : System::Web::UI::WebControls::BaseDataBoundControl
public abstract class HierarchicalDataBoundControl : System.Web.UI.WebControls.BaseDataBoundControl
type HierarchicalDataBoundControl = class
inherit BaseDataBoundControl
Public MustInherit Class HierarchicalDataBoundControl
Inherits BaseDataBoundControl
- 继承
- 派生
示例
下面的代码示例演示如何从 HierarchicalDataBoundControl 类派生类以创建自定义数据绑定控件。 该 GeneologyTree 控件呈现一个 pre HTML 节,其中包含从关联数据源控件检索到的数据的文本树。
using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Samples.AspNet.CS.Controls {
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
public class GeneologyTree : HierarchicalDataBoundControl {
private TreeNode rootNode;
public TreeNode RootNode {
get {
rootNode ??= new TreeNode(String.Empty);
return rootNode;
}
}
private ArrayList nodes;
public ArrayList Nodes {
get {
if (null == nodes) {
nodes = new ArrayList();
}
return nodes;
}
}
public string DataTextField {
get {
object o = ViewState["DataTextField"];
return((o == null) ? string.Empty : (string)o);
}
set {
ViewState["DataTextField"] = value;
if (Initialized) {
OnDataPropertyChanged();
}
}
}
private int _maxDepth = 0;
protected override void PerformDataBinding() {
base.PerformDataBinding();
// Do not attempt to bind data if there is no
// data source set.
if (!IsBoundUsingDataSourceID && (DataSource == null)) {
return;
}
HierarchicalDataSourceView view = GetData(RootNode.DataPath);
if (view == null) {
throw new InvalidOperationException
("No view returned by data source control.");
}
IHierarchicalEnumerable enumerable = view.Select();
if (enumerable != null) {
Nodes.Clear();
try {
RecurseDataBindInternal(RootNode, enumerable, 1);
}
finally {
}
}
}
private void RecurseDataBindInternal(TreeNode node,
IHierarchicalEnumerable enumerable, int depth) {
foreach(object item in enumerable) {
IHierarchyData data = enumerable.GetHierarchyData(item);
if (null != data) {
// Create an object that represents the bound data
// to the control.
TreeNode newNode = new TreeNode();
RootViewNode rvnode = new RootViewNode();
rvnode.Node = newNode;
rvnode.Depth = depth;
// The dataItem is not just a string, but potentially
// an XML node or some other container.
// If DataTextField is set, use it to determine which
// field to render. Otherwise, use the first field.
if (DataTextField.Length > 0) {
newNode.Text = DataBinder.GetPropertyValue
(data, DataTextField, null);
}
else {
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(data);
// Set the "default" value of the node.
newNode.Text = String.Empty;
// Set the true data-bound value of the TextBox,
// if possible.
if (props.Count >= 1) {
if (null != props[0].GetValue(data)) {
newNode.Text =
props[0].GetValue(data).ToString();
}
}
}
Nodes.Add(rvnode);
if (data.HasChildren) {
IHierarchicalEnumerable newEnumerable =
data.GetChildren();
if (newEnumerable != null) {
RecurseDataBindInternal(newNode,
newEnumerable, depth+1 );
}
}
if ( _maxDepth < depth) _maxDepth = depth;
}
}
}
protected override void Render(HtmlTextWriter writer) {
writer.WriteLine("<PRE>");
int currentDepth = 1;
int currentTextLen = 0;
foreach (RootViewNode rvnode in Nodes) {
if (rvnode.Depth == currentDepth) {
string output = " " + rvnode.Node.Text + " ";
writer.Write(output);
currentTextLen = currentTextLen + output.Length;
}
else {
writer.WriteLine("");
// Some very basic white-space formatting
int halfLine = currentTextLen / 2;
for (int i=0;i<halfLine;i++) {
writer.Write(' ');
}
writer.Write('|');
writer.WriteLine("");
++currentDepth;
currentTextLen = 0;
for (int j=0;j<halfLine;j++) {
writer.Write(' ');
}
string output = " " + rvnode.Node.Text + " ";
writer.Write(output);
currentTextLen = currentTextLen + output.Length;
}
}
writer.WriteLine("</PRE>");
}
private class RootViewNode {
public TreeNode Node;
public int Depth;
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal), _
AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class GeneologyTree
Inherits HierarchicalDataBoundControl
Dim MaxDepth As Integer = 0
Private aRootNode As TreeNode
Public ReadOnly Property RootNode() As TreeNode
Get
If aRootNode Is Nothing Then
aRootNode = New TreeNode(String.Empty)
End If
Return aRootNode
End Get
End Property
Private alNodes As ArrayList
Public ReadOnly Property Nodes() As ArrayList
Get
If alNodes Is Nothing Then
alNodes = New ArrayList()
End If
Return alNodes
End Get
End Property
Public Property DataTextField() As String
Get
Dim o As Object = ViewState("DataTextField")
If o Is Nothing Then
Return String.Empty
Else
Return CStr(o)
End If
End Get
Set(ByVal value As String)
ViewState("DataTextField") = value
If Initialized Then
OnDataPropertyChanged()
End If
End Set
End Property
Protected Overrides Sub PerformDataBinding()
MyBase.PerformDataBinding()
' Do not attempt to bind data if there is no
' data source set.
If Not IsBoundUsingDataSourceID AndAlso DataSource Is Nothing Then
Return
End If
Dim view As HierarchicalDataSourceView = GetData(RootNode.DataPath)
If view Is Nothing Then
Throw New InvalidOperationException _
("No view returned by data source control.")
End If
Dim enumerable As IHierarchicalEnumerable = view.Select()
If Not (enumerable Is Nothing) Then
Nodes.Clear()
Try
RecurseDataBindInternal(RootNode, enumerable, 1)
Finally
End Try
End If
End Sub
Private Sub RecurseDataBindInternal(ByVal node As TreeNode, _
ByVal enumerable As IHierarchicalEnumerable, _
ByVal depth As Integer)
Dim item As Object
For Each item In enumerable
Dim data As IHierarchyData = enumerable.GetHierarchyData(item)
If Not data Is Nothing Then
' Create an object that represents the bound data
' to the control.
Dim newNode As New TreeNode()
Dim rvnode As New RootViewNode()
rvnode.Node = newNode
rvnode.Depth = depth
' The dataItem is not just a string, but potentially
' an XML node or some other container.
' If DataTextField is set, use it to determine which
' field to render. Otherwise, use the first field.
If DataTextField.Length > 0 Then
newNode.Text = DataBinder.GetPropertyValue _
(data, DataTextField, Nothing)
Else
Dim props As PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(data)
' Set the "default" value of the node.
newNode.Text = String.Empty
' Set the true data-bound value of the TextBox,
' if possible.
If props.Count >= 1 Then
If Not props(0).GetValue(data) Is Nothing Then
newNode.Text = props(0).GetValue(data).ToString()
End If
End If
End If
Nodes.Add(rvnode)
If data.HasChildren Then
Dim newEnumerable As IHierarchicalEnumerable = _
data.GetChildren()
If Not (newEnumerable Is Nothing) Then
RecurseDataBindInternal(newNode, _
newEnumerable, depth + 1)
End If
End If
If MaxDepth < depth Then
MaxDepth = depth
End If
End If
Next item
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
writer.WriteLine("<PRE>")
Dim currentDepth As Integer = 1
Dim currentTextLen As Integer = 0
Dim rvnode As RootViewNode
For Each rvnode In Nodes
If rvnode.Depth = currentDepth Then
Dim output As String = " " + rvnode.Node.Text + " "
writer.Write(output)
currentTextLen = currentTextLen + output.Length
Else
writer.WriteLine("")
' Some very basic white-space formatting.
' The implicit conversion to an Integer is fine, as
' a general estimate is acceptable for this
' simple example.
Dim halfLine As Integer = CInt(currentTextLen / 2)
Dim i As Integer
For i = 0 To halfLine
writer.Write(" "c)
Next i
writer.Write("|"c)
writer.WriteLine("")
currentDepth += 1
Dim j As Integer
For j = 0 To halfLine
writer.Write(" "c)
Next j
Dim output As String = " " + rvnode.Node.Text + " "
writer.Write(output)
currentTextLen = currentTextLen + output.Length
End If
Next rvnode
writer.WriteLine("</PRE>")
End Sub
Private Class RootViewNode
Public Node As TreeNode
Public Depth As Integer
End Class
End Class
End Namespace
下面的代码示例演示如何使用 GeneologyTree 在上一示例中定义的控件,并将其绑定到控件 XmlDataSource 。
<%@Page language="c#" %>
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"
Assembly="Samples.AspNet.CS.Controls" %>
<!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>
<title>C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<aspSample:geneologytree
id="GeneologyTree1"
runat="server"
datatextfield="title"
datasourceid="XmlDataSource1" />
<asp:xmldatasource
id="XmlDataSource1"
datafile="geneology.xml"
runat="server" />
</form>
</body>
</html>
<%@Page language="VB" %>
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.VB.Controls"
Assembly="Samples.AspNet.VB.Controls" %>
<!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>
<title>VB Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<aspSample:geneologytree
id="GeneologyTree1"
runat="server"
datatextfield="title"
datasourceid="XmlDataSource1" />
<asp:xmldatasource
id="XmlDataSource1"
datafile="geneology.xml"
runat="server" />
</form>
</body>
</html>
在代码示例中访问的 geneology.xml 文件包含以下数据。
<family>
<member title="great-grandfather">
<member title="grandfather" >
<member title="child" />
<member title="father" >
<member title="son" />
</member>
</member>
</member>
</family>
注解
HierarchicalDataBoundControl 类是用于从 ASP.NET 分层数据源控件检索数据的 ASP.NET 控件的基类,并将控件的用户界面元素绑定到该数据以供显示。 和TreeViewMenu类派生自 HierarchicalDataBoundControl.
页面开发人员不直接使用该 HierarchicalDataBoundControl 类;而是使用派生自此类的控件。
控件开发人员扩展此类以创建数据绑定控件,这些控件使用实现 IHierarchicalDataSource 从和类派生的 HierarchicalDataSourceControl 接口和 HierarchicalDataSourceView 类的类。 从 HierarchicalDataBoundControl 类派生类时,重写 PerformDataBinding 方法以将控件的用户界面元素绑定到方法检索 GetData 的数据。 在大多数情况下,该方法 PerformDataBinding 是唯一将在派生类中重写的方法。
对于 ASP.NET 2.0 数据绑定控件,PerformSelect 方法等效于 DataBind 方法,在运行时调用该方法以绑定数据。 该方法 PerformSelect 调用 GetData 和 PerformDataBinding 方法。
构造函数
| 名称 | 说明 |
|---|---|
| HierarchicalDataBoundControl() |
初始化 HierarchicalDataBoundControl 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| AccessKey |
获取或设置访问密钥,使你能够快速导航到 Web 服务器控件。 (继承自 WebControl) |
| Adapter |
获取控件的特定于浏览器的适配器。 (继承自 Control) |
| AppRelativeTemplateSourceDirectory |
获取或设置包含此控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。 (继承自 Control) |
| Attributes |
获取不对应于控件上的属性的任意属性(仅用于呈现)的集合。 (继承自 WebControl) |
| BackColor |
获取或设置 Web 服务器控件的背景色。 (继承自 WebControl) |
| BindingContainer |
获取包含此控件的数据绑定的控件。 (继承自 Control) |
| BorderColor |
获取或设置 Web 控件的边框颜色。 (继承自 WebControl) |
| BorderStyle |
获取或设置 Web 服务器控件的边框样式。 (继承自 WebControl) |
| BorderWidth |
获取或设置 Web 服务器控件的边框宽度。 (继承自 WebControl) |
| ChildControlsCreated |
获取一个值,该值指示是否已创建服务器控件的子控件。 (继承自 Control) |
| ClientID |
获取由 ASP.NET 生成的 HTML 标记的控件 ID。 (继承自 Control) |
| ClientIDMode |
获取或设置用于生成属性值的 ClientID 算法。 (继承自 Control) |
| ClientIDSeparator |
获取一个表示属性中使用的 ClientID 分隔符的字符值。 (继承自 Control) |
| Context |
HttpContext获取与当前 Web 请求的服务器控件关联的对象。 (继承自 Control) |
| Controls |
获取一个 ControlCollection 对象,该对象代表 UI 层次结构中指定服务器控件的子控件。 (继承自 Control) |
| ControlStyle |
获取 Web 服务器控件的样式。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
| ControlStyleCreated |
获取一个值,该值 Style 指示是否已为 ControlStyle 属性创建对象。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
| CssClass |
获取或设置由客户端上的 Web 服务器控件呈现的级联样式表 (CSS) 类。 (继承自 WebControl) |
| DataItemContainer |
获取对命名容器的引用(如果命名容器实现 IDataItemContainer)。 (继承自 Control) |
| DataKeysContainer |
获取对命名容器的引用(如果命名容器实现 IDataKeysControl)。 (继承自 Control) |
| DataSource |
获取或设置数据绑定控件从中检索其数据项列表的对象。 (继承自 BaseDataBoundControl) |
| DataSourceID |
获取或设置数据绑定控件从中检索其数据项列表的控件的 ID。 |
| DesignMode |
获取一个值,该值指示控件是否在设计图面上使用。 (继承自 Control) |
| Enabled |
获取或设置一个值,该值指示是否启用 Web 服务器控件。 (继承自 WebControl) |
| EnableTheming |
获取或设置一个值,该值指示主题是否应用于此控件。 (继承自 WebControl) |
| EnableViewState |
获取或设置一个值,该值指示服务器控件是否将视图状态及其包含的任何子控件的视图状态保存到请求客户端。 (继承自 Control) |
| Events |
获取控件的事件处理程序委托的列表。 此属性为只读。 (继承自 Control) |
| Font |
获取与 Web 服务器控件关联的字体属性。 (继承自 WebControl) |
| ForeColor |
获取或设置 Web 服务器控件的前景色(通常是文本的颜色)。 (继承自 WebControl) |
| HasAttributes |
获取一个值,该值指示控件是否设置了属性。 (继承自 WebControl) |
| HasChildViewState |
获取一个值,该值指示当前服务器控件的子控件是否具有保存的视图状态设置。 (继承自 Control) |
| Height |
获取或设置 Web 服务器控件的高度。 (继承自 WebControl) |
| ID |
获取或设置分配给服务器控件的编程标识符。 (继承自 Control) |
| IdSeparator |
获取用于分隔控件标识符的字符。 (继承自 Control) |
| Initialized |
获取一个值,该值指示是否已初始化数据绑定控件。 (继承自 BaseDataBoundControl) |
| IsBoundUsingDataSourceID |
获取一个值,该值指示是否已 DataSourceID 设置属性。 (继承自 BaseDataBoundControl) |
| IsChildControlStateCleared |
获取一个值,该值指示此控件中包含的控件是否具有控件状态。 (继承自 Control) |
| IsDataBindingAutomatic |
获取一个值,该值指示数据绑定是否自动。 (继承自 BaseDataBoundControl) |
| IsEnabled |
获取一个值,该值指示是否启用控件。 (继承自 WebControl) |
| IsTrackingViewState |
获取一个值,该值指示服务器控件是否正在保存对其视图状态的更改。 (继承自 Control) |
| IsUsingModelBinders |
在派生类中实现时,获取一个值,该值指示控件是否使用模型绑定器。 (继承自 BaseDataBoundControl) |
| IsViewStateEnabled |
获取一个值,该值指示是否为此控件启用视图状态。 (继承自 Control) |
| LoadViewStateByID |
获取一个值,该值指示控件是否参与加载其视图状态,而不是 ID 索引。 (继承自 Control) |
| NamingContainer |
获取对服务器控件命名容器的引用,该容器创建唯一的命名空间,用于区分具有相同 ID 属性值的服务器控件。 (继承自 Control) |
| Page |
获取对 Page 包含服务器控件的实例的引用。 (继承自 Control) |
| Parent |
获取对页面控件层次结构中服务器控件的父控件的引用。 (继承自 Control) |
| RenderingCompatibility |
获取一个值,该值指定呈现的 HTML 将与 ASP.NET 版本兼容。 (继承自 Control) |
| RequiresDataBinding |
获取或设置一个值,该值指示是否 DataBind() 应调用该方法。 (继承自 BaseDataBoundControl) |
| Site |
获取有关在设计图面上呈现时承载当前控件的容器的信息。 (继承自 Control) |
| SkinID |
获取或设置要应用于控件的皮肤。 (继承自 WebControl) |
| Style |
获取文本属性的集合,该属性将在 Web 服务器控件的外部标记上呈现为样式属性。 (继承自 WebControl) |
| SupportsDisabledAttribute |
获取一个值,该值指示当控件的属性为控件的属性时,控件是否应将呈现的 |
| TabIndex |
获取或设置 Web 服务器控件的选项卡索引。 (继承自 WebControl) |
| TagKey |
HtmlTextWriterTag获取与此 Web 服务器控件对应的值。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
| TagName |
获取控件标记的名称。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
| TemplateControl |
获取或设置对包含此控件的模板的引用。 (继承自 Control) |
| TemplateSourceDirectory |
获取包含当前服务器控件的 Page 虚拟 UserControl 目录。 (继承自 Control) |
| ToolTip |
获取或设置鼠标指针悬停在 Web 服务器控件上时显示的文本。 (继承自 WebControl) |
| UniqueID |
获取服务器控件的唯一分层限定标识符。 (继承自 Control) |
| ValidateRequestMode |
获取或设置一个值,该值指示控件是否检查浏览器的客户端输入是否存在潜在危险值。 (继承自 Control) |
| ViewState |
获取状态信息的字典,该字典允许跨同一页的多个请求保存和还原服务器控件的视图状态。 (继承自 Control) |
| ViewStateIgnoresCase |
获取一个值,该值指示对象是否 StateBag 不区分大小写。 (继承自 Control) |
| ViewStateMode |
获取或设置此控件的视图状态模式。 (继承自 Control) |
| Visible |
获取或设置一个值,该值指示服务器控件是否呈现为页面上的 UI。 (继承自 Control) |
| Width |
获取或设置 Web 服务器控件的宽度。 (继承自 WebControl) |
方法
活动
| 名称 | 说明 |
|---|---|
| DataBinding |
当服务器控件绑定到数据源时发生。 (继承自 Control) |
| DataBound |
在服务器控件绑定到数据源之后发生。 (继承自 BaseDataBoundControl) |
| Disposed |
在从内存中释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生命周期的最后阶段。 (继承自 Control) |
| Init |
在初始化服务器控件时发生,这是其生命周期中的第一步。 (继承自 Control) |
| Load |
在将服务器控件加载到对象中 Page 时发生。 (继承自 Control) |
| PreRender |
在 Control 加载对象但在呈现之前发生。 (继承自 Control) |
| Unload |
从内存中卸载服务器控件时发生。 (继承自 Control) |
显式接口实现
扩展方法
| 名称 | 说明 |
|---|---|
| EnablePersistedSelection(BaseDataBoundControl) |
已过时.
允许选择保留在支持选择和分页的数据控件中。 |
| FindDataSourceControl(Control) |
返回与指定控件的数据控件关联的数据源。 |
| FindFieldTemplate(Control, String) |
返回指定控件命名容器中指定列的字段模板。 |
| FindMetaTable(Control) |
返回包含数据控件的元表对象。 |