HierarchicalDataSourceView.Select 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得檢視中的所有資料項目清單。
public:
abstract System::Web::UI::IHierarchicalEnumerable ^ Select();
public abstract System.Web.UI.IHierarchicalEnumerable Select ();
abstract member Select : unit -> System.Web.UI.IHierarchicalEnumerable
Public MustOverride Function Select () As IHierarchicalEnumerable
傳回
資料項目的 IHierarchicalEnumerable 集合。
範例
下列程式碼範例示範如何在衍生自 HierarchicalDataSourceView 類別的 類別中覆寫 Select 方法,以從檔案系統擷取階層 FileSystemInfo 式資料。 基於安全性考慮,只有在使用 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
備註
方法會 Select IHierarchicalEnumerable 傳回目前檢視中的資料項目集合。 您可以呼叫 GetEnumerator 方法來取得 物件,並逐一 IEnumerator 查看資料項目的集合。