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

<BindableAttribute(False)> _ Public MustInherit Class HierarchicalDataSourceControl Inherits Control Implements IHierarchicalDataSource
[BindableAttribute(false)] public abstract class HierarchicalDataSourceControl : Control, IHierarchicalDataSource
[BindableAttribute(false)] public ref class HierarchicalDataSourceControl abstract : public Control, IHierarchicalDataSource

ASP.NET では、Web サーバー コントロールをデータにバインドして、データを一貫した方式で提供できるようにするコントロール データ バインディング アーキテクチャがサポートされています。データにバインドされる Web サーバー コントロールをデータ バインド コントロールといいます。また、そのバインディングを容易にするクラスをデータ ソース コントロールといいます。データ ソース コントロールは、ファイル、ストリーム、リレーショナル データベース、ビジネス オブジェクトなど、あらゆるデータ ソースを表すことができます。データ ソース コントロールにより、基になるデータのソースや形式にかかわらず、一貫した方式でデータがデータ バインド コントロールに提供されます。
階層データを表すデータ ソース コントロールは HierarchicalDataSourceControl クラスから派生します。データのリストまたはテーブルを表すデータ ソース コントロールについては DataSourceControl クラスからの派生になります。HierarchicalDataSourceControl クラスは、IHierarchicalDataSource インターフェイスの基本実装になります。このインターフェイスは、データ ソース コントロールに関連付けられた階層データ ソース ビュー オブジェクトを取得するメソッド GetHierarchicalView を 1 つ定義します。
データ ソース コントロールは、HierarchicalDataSourceControl オブジェクトに、そのオブジェクトに関連付けられたデータ ソース ビュー オブジェクトと呼ばれるビューを組み合わせたものと考えることができます。表形式のデータを表すデータ ソース コントロールは、通常 1 つの名前付きビューと関連付けられますが、HierarchicalDataSourceControl クラスでは、データ ソース コントロールが表す階層データのレベルごとのデータ ソース ビューをサポートします。階層データのレベルは一意の階層パスによって識別され、GetHierarchicalView メソッドの viewPath パラメータに渡されます。各 HierarchicalDataSourceView オブジェクトは、表される階層レベルに対するデータ ソース コントロールの機能を定義し、挿入、更新、削除、並べ替えなどの操作を実行します。
HierarchicalDataBoundControl クラスから派生した TreeView などの Web サーバー コントロールが、階層データ ソース コントロールを使用して、階層データとバインドします。
データ ソース コントロールは、宣言による永続化を有効にして、オプションで、状態管理を行うためのコントロールとして実装されます。データ ソース コントロールは視覚的に表示されないため、テーマをサポートしません。

抽象 HierarchicalDataSourceControl クラスと HierarchicalDataSourceView クラスを拡張し、IHierarchicalEnumerable インターフェイスと IHierarchyData インターフェイスを実装して、ファイル システム情報を取得する階層データ ソース コントロールを作成する方法を次のコード例に示します。FileSystemDataSource コントロールによって、Web サーバー コントロールは FileSystemInfo オブジェクトにバインドされ、基本ファイル システム情報を表示できます。この例の FileSystemDataSource クラスは、GetHierarchicalView メソッドの実装を提供しています。このメソッドによって、FileSystemDataSourceView オブジェクトが取得されます。FileSystemDataSourceView オブジェクトは、基のデータ ストレージからデータを取得します。この場合は、Web サーバー上のファイル システム情報になります。セキュリティ上の理由で、ファイル システム情報が表示されるのは、データ ソース コントロールが、ローカルホスト上で認証された状態で使用されており、そのデータ ソース コントロールを使用する Web フォーム ページが存在する仮想ディレクトリでのみ起動する場合に限られます。最後に、FileSystemDataSource が使用する FileSystemInfo オブジェクトをラップするための、IHierarchicalEnumerable と IHierarchyData を実装する 2 つのクラスが提供されています。
Imports System Imports System.Collections Imports System.IO Imports System.Runtime.InteropServices Imports System.Security.Permissions Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class FileSystemDataSource Inherits HierarchicalDataSourceControl Public Sub New() End Sub 'New ' Return a strongly typed view for the current data source control. Private view As FileSystemDataSourceView = Nothing Protected Overrides Function GetHierarchicalView(viewPath As String) As HierarchicalDataSourceView If view Is Nothing Then view = New FileSystemDataSourceView(viewPath) End If Return view End Function 'GetHierarchicalView End Class 'FileSystemDataSource ' The FileSystemDataSourceView class encapsulates the ' capabilities of the FileSystemDataSource data source control. Public Class FileSystemDataSourceView Inherits HierarchicalDataSourceView Private _viewPath As String Public Sub New(viewPath As String) ' This implementation of HierarchicalDataSourceView does not ' use the viewPath parameter but other implementations ' could make use of it for retrieving values. _viewPath = viewPath End Sub 'New ' Starting with the rootNode, recursively build a list of ' FileSystemInfo nodes, create FileSystemHierarchyData ' objects, add them all to the FileSystemHierarchicalEnumerable, ' and return the list. Public Overrides Function [Select]() As IHierarchicalEnumerable Dim currentRequest As HttpRequest = HttpContext.Current.Request ' SECURITY: There are many security issues that can be raised ' SECURITY: by exposing the file system structure of a Web server ' SECURITY: to an anonymous user in a limited trust scenario such as ' SECURITY: a Web page served on an intranet or the Internet. ' SECURITY: For this reason, the FileSystemDataSource only ' SECURITY: shows data when the HttpRequest is received ' SECURITY: from a local Web server. In addition, the data source ' SECURITY: does not display data to anonymous users. If currentRequest.IsAuthenticated AndAlso(currentRequest.UserHostAddress = "127.0.0.1" OrElse currentRequest.UserHostAddress = "::1") Then ' The ApplicationPath returns a physical path in VB, so do not MapPath. Dim rootPath As String = currentRequest.MapPath(currentRequest.ApplicationPath) Dim rootDirectory As New DirectoryInfo(rootPath) Dim fshe As New FileSystemHierarchicalEnumerable() Dim fsi As FileSystemInfo For Each fsi In rootDirectory.GetFileSystemInfos() fshe.Add(New FileSystemHierarchyData(fsi)) Next fsi Return fshe Else Throw New NotSupportedException("The FileSystemDataSource only " + "presents data in an authenticated, localhost context.") End If End Function 'Select End Class 'FileSystemDataSourceView ' A collection of FileSystemHierarchyData objects Public Class FileSystemHierarchicalEnumerable Inherits ArrayList Implements IHierarchicalEnumerable Public Sub New() End Sub 'New Public Overridable Function GetHierarchyData(enumeratedItem As Object) As IHierarchyData _ Implements IHierarchicalEnumerable.GetHierarchyData Return CType(enumeratedItem, IHierarchyData) End Function 'GetHierarchyData End Class 'FileSystemHierarchicalEnumerable Public Class FileSystemHierarchyData Implements IHierarchyData Public Sub New(obj As FileSystemInfo) fileSystemObject = obj End Sub 'New Private fileSystemObject As FileSystemInfo = Nothing Public Overrides Function ToString() As String Return fileSystemObject.Name End Function 'ToString ' IHierarchyData implementation. Public Overridable ReadOnly Property HasChildren() As Boolean _ Implements IHierarchyData.HasChildren Get If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then Dim temp As DirectoryInfo = CType(fileSystemObject, DirectoryInfo) Return temp.GetFileSystemInfos().Length > 0 Else Return False End If End Get ' DirectoryInfo returns the OriginalPath, while FileInfo returns ' a fully qualified path. Public Overridable ReadOnly Property Path() As String _ Implements IHierarchyData.Path Get Return fileSystemObject.ToString() End Get End Property Public Overridable ReadOnly Property Item() As Object _ Implements IHierarchyData.Item Get Return fileSystemObject End Get End Property Public Overridable ReadOnly Property Type() As String _ Implements IHierarchyData.Type Get Return "FileSystemData" End Get End Property Public Overridable Function GetChildren() As IHierarchicalEnumerable _ Implements IHierarchyData.GetChildren Dim children As New FileSystemHierarchicalEnumerable() If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then Dim temp As DirectoryInfo = CType(fileSystemObject, DirectoryInfo) Dim fsi As FileSystemInfo For Each fsi In temp.GetFileSystemInfos() children.Add(New FileSystemHierarchyData(fsi)) Next fsi End If Return children End Function 'GetChildren Public Overridable Function GetParent() As IHierarchyData _ Implements IHierarchyData.GetParent Dim parentContainer As New FileSystemHierarchicalEnumerable() If GetType(DirectoryInfo) Is fileSystemObject.GetType() Then Dim temp As DirectoryInfo = CType(fileSystemObject, DirectoryInfo) Return New FileSystemHierarchyData(temp.Parent) ElseIf GetType(FileInfo) Is fileSystemObject.GetType() Then Dim temp As FileInfo = CType(fileSystemObject, FileInfo) Return New FileSystemHierarchyData(temp.Directory) End If ' If FileSystemObj is any other kind of FileSystemInfo, ignore it. Return Nothing End Function 'GetParent End Class 'FileSystemHierarchyData End Namespace
using System; using System.Collections; using System.IO; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] public class FileSystemDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource { public FileSystemDataSource() : base() {} // Return a strongly typed view for the current data source control. private FileSystemDataSourceView view = null; protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath) { if (null == view) { view = new FileSystemDataSourceView(viewPath); } return view; } // The FileSystemDataSource can be used declaratively. To enable // declarative use, override the default implementation of // CreateControlCollection to return a ControlCollection that // you can add to. protected override ControlCollection CreateControlCollection() { return new ControlCollection(this); } } // The FileSystemDataSourceView class encapsulates the // capabilities of the FileSystemDataSource data source control. public class FileSystemDataSourceView : HierarchicalDataSourceView { private string _viewPath; public FileSystemDataSourceView(string viewPath) { // This implementation of HierarchicalDataSourceView does not // use the viewPath parameter but other implementations // could make use of it for retrieving values. _viewPath = viewPath; } // Starting with the rootNode, recursively build a list of // FileSystemInfo nodes, create FileSystemHierarchyData // objects, add them all to the FileSystemHierarchicalEnumerable , // and return the list. public override IHierarchicalEnumerable Select() { HttpRequest currentRequest = HttpContext.Current.Request; // SECURITY: There are many security issues that can be raised // SECURITY: by exposing the file system structure of a Web server // SECURITY: to an anonymous user in a limited trust scenario such as // SECURITY: a Web page served on an intranet or the Internet. // SECURITY: For this reason, the FileSystemDataSource only // SECURITY: shows data when the HttpRequest is received // SECURITY: from a local Web server. In addition, the data source // SECURITY: does not display data to anonymous users. if ( currentRequest.IsAuthenticated && (currentRequest.UserHostAddress == "127.0.0.1" || currentRequest.UserHostAddress == "::1")) { string rootPath = currentRequest.MapPath (currentRequest.ApplicationPath); DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); FileSystemHierarchicalEnumerable fshe = new FileSystemHierarchicalEnumerable(); foreach (FileSystemInfo fsi in rootDirectory.GetFileSystemInfos()) { fshe.Add(new FileSystemHierarchyData(fsi)); } return fshe; } else { throw new NotSupportedException("The FileSystemDataSource only " + "presents data in an authenticated, localhost context."); } } } // A collection of FileSystemHierarchyData objects public class FileSystemHierarchicalEnumerable : ArrayList, IHierarchicalEnumerable { public FileSystemHierarchicalEnumerable () : base (){ } public IHierarchyData GetHierarchyData(object enumeratedItem) { return enumeratedItem as IHierarchyData; } } public class FileSystemHierarchyData : IHierarchyData { public FileSystemHierarchyData (FileSystemInfo obj) { fileSystemObject = obj; } private FileSystemInfo fileSystemObject = null; public override string ToString() { return fileSystemObject.Name; } // IHierarchyData implementation. public bool HasChildren { get { if (typeof(DirectoryInfo) == fileSystemObject.GetType() ) { DirectoryInfo temp = (DirectoryInfo) fileSystemObject; return (temp.GetFileSystemInfos().Length > 0); } else return false; } } // DirectoryInfo returns the OriginalPath, while FileInfo returns // a fully qualified path. public string Path { get { return fileSystemObject.ToString(); } } public object Item { get { return fileSystemObject; } } public string Type { get { return "FileSystemData"; } } public IHierarchicalEnumerable GetChildren() { FileSystemHierarchicalEnumerable children = new FileSystemHierarchicalEnumerable(); if (typeof(DirectoryInfo) == fileSystemObject.GetType()) { DirectoryInfo temp = (DirectoryInfo)fileSystemObject; foreach (FileSystemInfo fsi in temp.GetFileSystemInfos()) { children.Add(new FileSystemHierarchyData(fsi)); } } return children; } public IHierarchyData GetParent() { FileSystemHierarchicalEnumerable parentContainer = new FileSystemHierarchicalEnumerable(); if (typeof(DirectoryInfo) == fileSystemObject.GetType()) { DirectoryInfo temp = (DirectoryInfo)fileSystemObject; return new FileSystemHierarchyData(temp.Parent); } else if (typeof(FileInfo) == fileSystemObject.GetType()) { FileInfo temp = (FileInfo)fileSystemObject; return new FileSystemHierarchyData(temp.Directory); } // If FileSystemObj is any other kind of FileSystemInfo, ignore it. return null; } }
FileSystemDataSource の例を使用して、宣言により TreeView コントロールをファイル システム データにバインドする方法を次のコード例に示します。
Imports System Imports System.Collections Imports System.IO Imports System.Runtime.InteropServices Imports System.Security.Permissions Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.VB <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class FileSystemDataSource Inherits HierarchicalDataSourceControl Public Sub New() End Sub 'New ' Return a strongly typed view for the current data source control. Private view As FileSystemDataSourceView = Nothing Protected Overrides Function GetHierarchicalView(viewPath As String) As HierarchicalDataSourceView If view Is Nothing Then view = New FileSystemDataSourceView(viewPath) End If Return view End Function 'GetHierarchicalView End Class 'FileSystemDataSource
using System; using System.Collections; using System.IO; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] public class FileSystemDataSource : HierarchicalDataSourceControl, IHierarchicalDataSource { public FileSystemDataSource() : base() {} // Return a strongly typed view for the current data source control. private FileSystemDataSourceView view = null; protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath) { if (null == view) { view = new FileSystemDataSourceView(viewPath); } return view; } // The FileSystemDataSource can be used declaratively. To enable // declarative use, override the default implementation of // CreateControlCollection to return a ControlCollection that // you can add to. protected override ControlCollection CreateControlCollection() { return new ControlCollection(this); } }


System.Web.UI.Control
System.Web.UI.HierarchicalDataSourceControl
System.Web.UI.WebControls.SiteMapDataSource
System.Web.UI.WebControls.XmlDataSource


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- HierarchicalDataSourceControl クラスのページへのリンク