2009-05-25 27 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部分”好文章