2009-05-25 63 views
2

開始查看Treeview控件。TreeView中的目錄結構VB

有沒有辦法將樹視圖控件綁定到使用Visual Basic的Web服務器上的目錄結構中?

我有很多舊文件,這些文件經常更新和添加。很明顯,我可以用XML編碼結構,但這很費力,很難培訓給最終用戶。

我想這應該是一個XML文件的動態創建?

回答

3

下面是我學習用的TreeView玩的時候創造了前一段時間的基本樣本。我現在使用online converter將代碼轉換爲VB.NET。

遞歸地從虛擬目錄的根開始遍歷目錄樹,併爲每個遇到的子目錄或文件創建節點。我認爲這正是你需要的。

對於視覺分離,我用圖標來區分文件夾(folder.gif和file.gif)。如果需要,您可以刪除該參數。

完全ASPX如下(你可以將其粘貼到一個新的頁面,它應該運行):


<%@ Page Language="VB" %> 
<%@ Import Namespace="System.IO" %> 

<script runat="server"> 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    If Not Page.IsPostBack Then 
     Dim rootDir As New DirectoryInfo(Server.MapPath("~/")) 

     ' Enter the RecurseNodes function to recursively walk the directory tree. 
     Dim RootNode As TreeNode = RecurseNodes(rootDir) 

     ' Add this Node hierarchy to the TreeNode control. 
     Treeview1.Nodes.Add(RootNode) 
    End If 
    End Sub 

    Private Function RecurseNodes(ByVal thisDir As DirectoryInfo) As TreeNode 
    Dim thisDirNode As New TreeNode(thisDir.Name, Nothing, "Images/folder.gif") 

    ' Get all the subdirectories in this Directory. 
    Dim subDirs As DirectoryInfo() = thisDir.GetDirectories() 
    For Each subDir As DirectoryInfo In subDirs 
     thisDirNode.ChildNodes.Add(RecurseNodes(subDir)) 
    Next 

    ' Now get the files in this Directory. 
    Dim files As FileInfo() = thisDir.GetFiles() 
    For Each file As FileInfo In files 
     Dim thisFileNode As New TreeNode(file.Name, Nothing, "Images/file.gif") 
     thisDirNode.ChildNodes.Add(thisFileNode) 
    Next 

    Return thisDirNode 
    End Function 
</script> 

<html> 
<head> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:treeview ID="Treeview1" runat="server"></asp:treeview> 
    </form> 
</body> 
</html> 
+0

爵士, 這是驚人的,這麼簡單。許多非常感謝 – 2009-05-28 21:48:26

2

自定義站點地圖提供程序是一個不錯的選擇。

上有4guys標題「檢查ASP.NET 2.0的站點導航 - 第4部分」好文章