HierarchicalDataSourceView 类

定义

表示 HierarchicalDataSourceControl 控件的分层数据结构中的节点或节点集合上的数据视图。Represents a data view on a node or collection of nodes in a hierarchical data structure for a HierarchicalDataSourceControl control.

public ref class HierarchicalDataSourceView abstract
public abstract class HierarchicalDataSourceView
type HierarchicalDataSourceView = class
Public MustInherit Class HierarchicalDataSourceView
继承
HierarchicalDataSourceView
派生

示例

下面的代码示例演示如何从HierarchicalDataSourceView类派生一个类, 以从分层数据存储 (在本例中为文件系统) 中检索数据。The following code example demonstrates how to derive a class from the HierarchicalDataSourceView class to retrieve data from hierarchical data storage, in this case a file system. 类是一个强类型HierarchicalDataSourceView的实例, 它使分层 Web TreeView服务器控件 (如控件) 可以绑定到FileSystemDataSource控件并显示文件系统信息。 FileSystemDataSourceViewThe FileSystemDataSourceView class is a strongly typed HierarchicalDataSourceView instance that enables hierarchical Web server controls such as the TreeView control to bind to a FileSystemDataSource control and display file system information. 出于安全目的, 仅当数据源控件正用于本地主机和身份验证方案时才会显示文件系统信息, 并且仅从使用数据源控件的 Web 窗体页所在的虚拟目录开始。For security purposes, file system information is displayed only if the data source control is being used in a localhost, authenticated scenario, and only starts with the virtual directory that the Web Forms page using the data source control resides in. 否则, 传递viewPath给构造函数的参数可能用于基于当前文件系统路径创建视图。Otherwise, the viewPath parameter passed to the constructor might be used to create a view based on the current file system path. 此代码示例是为HierarchicalDataSourceControl类提供的更大示例的一部分。This code example is part of a larger example provided for the HierarchicalDataSourceControl class.

public class FileSystemDataSourceView : HierarchicalDataSourceView
{
    private string _viewPath;

    public FileSystemDataSourceView(string viewPath)
    {
        HttpRequest currentRequest = HttpContext.Current.Request;
        if (viewPath == "")
        {
            _viewPath = currentRequest.MapPath(currentRequest.ApplicationPath);
        }
        else
        {
            _viewPath = Path.Combine(
                currentRequest.MapPath(currentRequest.ApplicationPath),
                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"))
        {
            DirectoryInfo rootDirectory = new DirectoryInfo(_viewPath);
            if (!rootDirectory.Exists)
            {
                return null;
            }

            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.");
        }
    }
}
Public Class FileSystemDataSourceView
    Inherits HierarchicalDataSourceView

    Private _viewPath As String

    Public Sub New(ByVal viewPath As String)
        Dim currentRequest As HttpRequest = HttpContext.Current.Request
        If viewPath = "" Then
            _viewPath = currentRequest.MapPath(currentRequest.ApplicationPath)
        Else
            _viewPath = Path.Combine(currentRequest.MapPath(currentRequest.ApplicationPath), viewPath)
        End If
    End Sub


    ' 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

            Dim rootDirectory As New DirectoryInfo(_viewPath)

            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

注解

ASP.NET 支持一种数据绑定体系结构, 使 Web 服务器控件可以绑定到数据并以一致的方式呈现数据。ASP.NET supports a data-binding architecture that enables Web server controls to bind to data and present it in a consistent fashion. 绑定到数据的 Web 服务器控件称为 "数据绑定" 控件, 用于简化绑定的类称为数据源控件。Web server controls that bind to data are called data-bound controls, and the classes that facilitate that binding are called data source controls. 数据源控件可以表示任意数据源: 文件、流、关系数据库、业务对象等。Data source controls can represent any data source: a file, a stream, a relational database, a business object, and so on. 数据源控件以一致的方式向数据绑定控件提供数据, 而与基础数据的源或格式无关。Data source controls present data in a consistent way to data-bound controls, regardless of the source or format of the underlying data.

表示分层数据的数据源控件派生自抽象HierarchicalDataSourceControl类。Data source controls that represent hierarchical data derive from the abstract HierarchicalDataSourceControl class. 您可以将数据源控件视为数据源控件对象及其关联视图对基础数据的组合, 这些数据由数据源视图对象表示。You can think of a data source control as the combination of the data source control object and its associated views on the underlying data, which are represented by data source view objects. 分层数据源控件支持其所表示的每个层次结构级别的分层数据源视图。Hierarchical data source controls support a hierarchical data source view for each hierarchical level of data they represent. 数据源视图的命名方式不同于DataSourceView DataSourceControl与控件关联的对象, 但由其唯一的分层路径标识。The data source views are not named, like the DataSourceView objects associated with a DataSourceControl control, but are identified by their unique hierarchical path.

数据源视图定义数据源控件的功能。Data source views define the capabilities of a data source control. 所有数据源视图对象 (包括HierarchicalDataSourceView) 都支持Select使用方法从基础数据源中检索数据, 该方法将分层IHierarchicalEnumerable列表的数据作为对象进行检索。All data source view objects, including HierarchicalDataSourceView, support data retrieval from the underlying data source using the Select method, which retrieves a hierarchical list of data as an IHierarchicalEnumerable object. 所有数据源视图对象都可以支持一组基本的功能, 包括Insert DeleteUpdate、和排序等操作。All data source view objects optionally support a basic set of capabilities, including operations such as Insert, Update, Delete, and sorting. 数据绑定控件可以通过使用GetHierarchicalView方法检索关联的数据源视图, 并在设计时或运行时查询视图来发现数据源控件的功能。A data-bound control can discover the capabilities of a data source control by retrieving an associated data source view using the GetHierarchicalView method and querying the view at design time or at run time. HierarchicalDataSourceView当前不支持Insert UpdateDelete操作。HierarchicalDataSourceView does not currently support Insert, Update or Delete operations.

实施者说明

HierarchicalDataSourceView 继承时,必须重写以下成员:Select()When you inherit from HierarchicalDataSourceView, you must override the following member: Select().

构造函数

HierarchicalDataSourceView()

初始化 HierarchicalDataSourceView 类的新实例。Initializes a new instance of the HierarchicalDataSourceView class.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Select()

获取视图中所有数据项的列表。Gets a list of all the data items in the view.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅