Ok, so we will assume you have a simple database table with some folders in it.
Say, like this:
Ok, so now we need to take above from database, and fill out the tree view.
of course when a user "expands" the treeview, we will list out files (and or folders) in that folder.
So, drop in a treeview, say this markup:
<h3>My Folders</h3>
<asp:TreeView ID="TreeView1" runat="server"
NodeIndent="35"
OnTreeNodeExpanded="TreeView1_TreeNodeExpanded">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="12pt" ForeColor="Black"
HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>
Ok, so now our code to load from database:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadDBFolders()
End If
End Sub
Sub LoadDBFolders()
Dim rstFolders As DataTable
rstFolders = MyRst("SELECT * FROM tblFolders ORDER BY Folder")
' add this list of folders to the tree.
TreeView1.ExpandImageUrl = "~/Content/blueclose2.png"
TreeView1.CollapseImageUrl = "~/Content/blueopen2.png"
TreeView1.NoExpandImageUrl = "~/Content/file2.png"
For Each OneRow As DataRow In rstFolders.Rows
' lets ONLY display the folder name, no path, no matter where folder is
Dim FoldersPath As String() = Split(OneRow("Folder"), "\")
Dim strTopFolder As String = FoldersPath(FoldersPath.Length - 1)
Dim child As New TreeNode
child.Text = strTopFolder ' we display JUST the top folder name
child.Value = OneRow("Folder") ' we Store/save full path name
child.Expanded = False
child.PopulateOnDemand = True
'child.ShowCheckBox = False
TreeView1.Nodes.Add(child)
Next
End Sub
So, that loads up the tree view.
However, now the "more" challenging part. When user clicks on a folder, we need to expand the tree view to the file system.
So, then this bit of code works quite nice:
I drop this code right below above code:
Public Class MyFolder
Public MyFiles As FileInfo()
Public MyFolders As DirectoryInfo()
End Class
Public Function GetFiles(sRootFolder As String) As MyFolder
Dim MyDir As New DirectoryInfo(sRootFolder)
Dim cMyFolder As New MyFolder
cMyFolder.MyFolders = MyDir.GetDirectories("*.*", SearchOption.TopDirectoryOnly)
cMyFolder.MyFiles = MyDir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
Return cMyFolder
End Function
Protected Sub TreeView1_TreeNodeExpanded(sender As Object, e As TreeNodeEventArgs)
' user clicked on tree node, get the folder, and fill out files
' and sub folders too!!!
Dim child As TreeNode = e.Node
Dim dtChild As MyFolder = GetFiles(child.Value) ' get ALL files + folders
LoadTreeFiles(child.Value, dtChild, child)
End Sub
Sub LoadTreeFiles(fRootL As String,
lParent As MyFolder,
tTreeNode As TreeNode)
' add folders if any
tTreeNode.ChildNodes.Clear()
For Each sFolder As DirectoryInfo In lParent.MyFolders
Dim child As New TreeNode
child.Text = sFolder.Name
child.Value = sFolder.FullName
child.Expanded = False
child.PopulateOnDemand = True
child.ShowCheckBox = False
tTreeNode.ChildNodes.Add(child)
Next
' now any files
For Each sFile As FileInfo In lParent.MyFiles
Dim child2 As New TreeNode
child2.Text = sFile.Name
child2.Value = sFile.FullName
child2.Expanded = False
child2.ShowCheckBox = True
child2.PopulateOnDemand = False
tTreeNode.ChildNodes.Add(child2)
Next
End Sub
And the result is now this: