HierarchicalDataBoundControl イベント

名前 | 説明 | |
---|---|---|
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。 ( Control から継承されます。) |
![]() | DataBound | サーバー コントロールがデータ ソースにバインドした後に発生します。 ( BaseDataBoundControl から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。 ( Control から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。 ( Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。 ( Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。 ( Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。 ( Control から継承されます。) |

HierarchicalDataBoundControl クラス
アセンブリ: System.Web (system.web.dll 内)


HierarchicalDataBoundControl クラスは、ASP.NET データ ソース コントロールから階層形式のデータを取得し、コントロールのユーザー インターフェイス要素をそのデータにバインドして表示する ASP.NET コントロールに使用される基本クラスです。TreeView クラスおよび Menu クラスは、HierarchicalDataBoundControl から派生します。
ページ開発者は、HierarchicalDataBoundControl クラスを直接使用せずに、このクラスから派生するコントロールを使用します。
コントロール開発者はこのクラスを拡張して、IHierarchicalDataSource インターフェイスを実装するクラス、および HierarchicalDataSourceControl クラスと HierarchicalDataSourceView クラスから派生するクラスで機能するデータ バインド コントロールを作成します。HierarchicalDataBoundControl クラスから派生クラスを作成する場合、PerformDataBinding メソッドをオーバーライドして、コントロールのユーザー インターフェイス要素を GetData メソッドで取得したデータにバインドします。ほとんどの場合、派生クラスでオーバーライドするメソッドは、PerformDataBinding メソッドだけです。
ASP.NET 2.0 データ バインド コントロールの場合、PerformSelect メソッドは DataBind メソッドと等価で、実行時にデータをバインドするために呼び出されます。PerformSelect メソッドは、GetData メソッドと PerformDataBinding メソッドを呼び出します。

HierarchicalDataBoundControl クラスから派生クラスを作成して、カスタムのデータ バインド コントロールを作成する方法を次のコード例に示します。GeneologyTree コントロールは、HTML の pre セクションを、関連付けられたデータ ソース コントロールから取得したデータのテキスト ツリーで描画します。
Imports System 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 ' PerformDataBinding 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 'RecurseDataBindInternal 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 whitespace 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 'Render Private Class RootViewNode Public Node As TreeNode Public Depth As Integer End Class 'RootViewNode End Class 'GeneologyTree End Namespace
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 { if (rootNode == null) { 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 whitespace 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; } } }
前の例で定義した GeneologyTree コントロールを使用して、XmlDataSource コントロールにバインドする方法を次のコード例に示します。
<%@Page language="VB" %> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB.Controls" %> <html> <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>
<%@Page language="c#" %> <%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS.Controls" %> <html> <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>


System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.HierarchicalDataBoundControl
System.Web.UI.WebControls.Menu
System.Web.UI.WebControls.TreeView


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


HierarchicalDataBoundControl コンストラクタ
アセンブリ: System.Web (system.web.dll 内)


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


HierarchicalDataBoundControl プロパティ



HierarchicalDataBoundControl メソッド



HierarchicalDataBoundControl メンバ
データを階層形式で表示するすべての ASP.NET Version 2.0 データ バインド コントロールの基本クラスとして機能します。
HierarchicalDataBoundControl データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | HierarchicalDataBoundControl |





名前 | 説明 | |
---|---|---|
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。(Control から継承されます。) |
![]() | DataBound | サーバー コントロールがデータ ソースにバインドした後に発生します。(BaseDataBoundControl から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。(Control から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。(Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。(Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。(Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。(Control から継承されます。) |

Weblioに収録されているすべての辞書からHierarchicalDataBoundControlを検索する場合は、下記のリンクをクリックしてください。

- HierarchicalDataBoundControlのページへのリンク