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. FileSystemDataSourceView類別是強型別 HierarchicalDataSourceView 實例,可啟用階層式 Web 服務器控制項(例如控制項),以系結 TreeViewFileSystemDataSource 控制項並顯示檔案系統資訊。The 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. 基於安全性目的,只有在 localhost、已驗證的案例中使用資料來源控制項,而且只從使用資料來源控制項的 Web form 頁面所在的虛擬目錄開始時,才會顯示檔案系統資訊。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. 這個程式碼範例是針對類別提供之較大範例的一部分 HierarchicalDataSourceControlThis 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. 您可以將資料來源控制項視為資料來源控制物件的組合,以及其在基礎資料上相關聯的視圖,這是由資料來源 view 物件所表示。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. 資料來源 views 不會命名,像是 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. 所有的資料來源 view 物件(包括 HierarchicalDataSourceView )都支援使用方法從基礎資料來源抓取資料 Select ,這會以物件的形式抓取資料的階層式清單 IHierarchicalEnumerableAll 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. 所有資料來源 view 物件都可選擇性地支援一組基本的功能,包括 InsertUpdateDelete 和排序等作業。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()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱