HierarchicalDataSourceView 類別

定義

以階層式資料結構,表示 HierarchicalDataSourceControl 控制項的節點或節點集合的資料檢視。

public ref class HierarchicalDataSourceView abstract
public abstract class HierarchicalDataSourceView
type HierarchicalDataSourceView = class
Public MustInherit Class HierarchicalDataSourceView
繼承
HierarchicalDataSourceView
衍生

範例

下列程式碼範例示範如何從 HierarchicalDataSourceView 類別衍生類別,以從階層式資料儲存體擷取資料,在此案例中為檔案系統。 類別 FileSystemDataSourceView 是強型別 HierarchicalDataSourceView 的實例,可讓階層式 Web 服務器控制項,例如 TreeView 控制項系結至 FileSystemDataSource 控制項並顯示檔案系統資訊。 基於安全性考慮,只有在使用 localhost、已驗證的案例中使用資料來源控制項時,才會顯示檔案系統資訊,而且只會以使用資料來源控制項之Web Form頁面所在的虛擬目錄開始。 否則, viewPath 傳遞至建構函式的參數可能會用來根據目前的檔案系統路徑建立檢視。 此程式碼範例是提供給 類別之較大範例的 HierarchicalDataSourceControl 一部分。

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 服務器控制項系結至資料並以一致的方式呈現資料。 系結至資料的 Web 服務器控制項稱為資料繫結控制項,以及有助於系結的類別稱為資料來源控制項。 資料來源控制項可以代表任何資料來源:檔案、資料流程、關係資料庫、商務物件等等。 不論基礎資料的來源或格式為何,資料來源控制項都會以一致的方式呈現資料給資料繫結控制項。

代表階層式資料的資料來源控制項衍生自抽象 HierarchicalDataSourceControl 類。 您可以將資料來源控制項視為資料來源控制項物件及其基礎資料相關檢視的組合,這些檢視是由資料來源檢視物件表示。 階層式資料來源控制項支援其所代表之每個階層式資料層級的階層式資料來源檢視。 資料來源檢視不會命名,就像與控制項相關聯的物件一樣 DataSourceView ,而是由唯一 DataSourceControl 的階層式路徑來識別。

資料來源檢視會定義資料來源控制項的功能。 所有資料來源檢視物件,包括 HierarchicalDataSourceView ,都支援使用 Select 方法從基礎資料來源擷取資料,以物件的形式 IHierarchicalEnumerable 擷取階層式資料清單。 所有資料來源檢視物件選擇性地支援一組基本功能,包括 、 UpdateDelete 和 排序等 Insert 作業。 資料繫結控制項可以使用 方法擷取相關聯的資料來源檢視,並在設計階段或執行時間查詢檢視 GetHierarchicalView ,來探索資料來源控制項的功能。 HierarchicalDataSourceView 目前不支援 InsertUpdateDelete 作業。

給實施者的注意事項

當您從 HierarchicalDataSourceView 繼承時,必須覆寫下列成員:Select()

建構函式

HierarchicalDataSourceView()

初始化 HierarchicalDataSourceView 類別的新執行個體。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Select()

取得檢視中的所有資料項目清單。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱