2009-05-25 20 views
2

ツリービューコントロールの表示を開始しました。TreeView内のディレクトリ構造VB

Visual Basicを使用してWebサーバー上のディレクトリ構造にツリービューコントロールを結びつけるにはどうしてですか?

多くのレガシーファイルがありますが、頻繁に更新され追加されています。明らかにXMLで構造をコーディングすることはできますが、これは面倒であり、エンドユーザーにトレーニングするのは難しいでしょう。

おそらく、XMLファイルを動的に作成すると思いますか?

答えて

3

はここツリービューで遊ぶことを学ぶとき、私はしばらく前に作成した基本サンプルですが。私はあなたの利益のために今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

卿、 これは素晴らしい、と非常にシンプルです。多くの多くのありがとう –

+0

それは私の喜びであり、あなたは大歓迎です。 :-) – Cerebrus

2

カスタムサイトマッププロバイダは良い選択です。

「ASP.NET 2.0のサイトナビゲーションの検証 - パート4」4guysタイトルに良い記事があり

関連する問題