

Navigator : Home > Tutorials > Controls Tutorials > ...
TreeView without page refresh by ASP.NET 2.0 and VB.NET
This tutorial will show you how to work with TreeView control to display folders and file names without refreshing web page by VB.NET and ASP.NET 2.0.
This tutorial will show you how to work with TreeView control to display folders and file names without refreshing web page by VB.NET and ASP.NET 2.0.
First, you need to import the namespace from System.IO.DirectoryInfo in the namespace System.IO.DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
We will use the Treeview control to display the files names and folders without refreshing web page. The TreeView is an ASP.NET server control for authoring user interfaces representing hierarchical data. In the sample, we will use the events of Treeview1_TreeNodePopulate and LoadChildNode to show you how it works. Treeview1_TreeNodePopulate is used for displaying the folders and files in drive C. LoadChildNode is used for loading the name of folder and file. The code as below:
Protected Sub Treeview1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles Treeview1.TreeNodePopulate
If IsCallback = True Then
If e.Node.ChildNodes.Count = 0 Then
LoadChildNode(e.Node) End If End If End Sub
Private Sub LoadChildNode(ByVal node As TreeNode)
Dim directory As DirectoryInfo directory = New DirectoryInfo(node.Value) For Each subtree As DirectoryInfo In directory.GetDirectories()
Dim subNode As TreeNode = New TreeNode(subtree.Name) subNode.Value = subtree.FullName Try
If subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then
subNode.SelectAction = TreeNodeSelectAction.SelectExpand subNode.PopulateOnDemand = True subNode.NavigateUrl = "#" End If Catch ex As Exception End Try node.ChildNodes.Add(subNode) Next For Each fi As FileInfo In directory.GetFiles()
Dim subNode As TreeNode = New TreeNode(fi.Name) node.ChildNodes.Add(subNode) Next End Sub |
The front end TreeViewVB2005.aspx page looks something like this:
<asp:treeview ID="Treeview1" runat="server" ImageSet="XPFileExplorer" AutoGenerateDataBindings="false" ExpandDepth=0 OnTreeNodePopulate="Treeview1_TreeNodePopulate" > <SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle> <Nodes><asp:TreeNode Value="C:" Text="C:" PopulateOnDemand="true" SelectAction="Select" NavigateUrl="#" > </asp:TreeNode> </Nodes> <NodeStyle VerticalPadding="2" Font-Names="Tahoma" Font-Size="8pt" HorizontalPadding="2" ForeColor="Black"></NodeStyle> <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"> </HoverNodeStyle> </asp:treeview> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
The flow for the code behind page is as follows
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Treeview1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles Treeview1.TreeNodePopulate
If IsCallback = True Then
If e.Node.ChildNodes.Count = 0 Then
LoadChildNode(e.Node) End If End If End Sub Private Sub LoadChildNode(ByVal node As TreeNode)
Dim directory As DirectoryInfo directory = New DirectoryInfo(node.Value) For Each subtree As DirectoryInfo In directory.GetDirectories()
Dim subNode As TreeNode = New TreeNode(subtree.Name) subNode.Value = subtree.FullName Try
If subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then
subNode.SelectAction = TreeNodeSelectAction.SelectExpand subNode.PopulateOnDemand = True subNode.NavigateUrl = "#" End If Catch ex As Exception End Try node.ChildNodes.Add(subNode) Next For Each fi As FileInfo In directory.GetFiles()
Dim subNode As TreeNode = New TreeNode(fi.Name) node.ChildNodes.Add(subNode) Next End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!