How to display Files and folders in a hierarchical structure using ASP.NET Treeview control
ASP.Net Treeview control is a server control used to design data in a hierarchal structure. Here we are using Treeview control to display folders and files in a tree structure. Normally we use recursive function to accomplish this task. TreeView structure in ASP.NET helps you achieve this in few lines of code.
If you wanted to display Files and Folders in hierarchical structure the best control available in ASP.NET is TreeView Control. Here we will see a simple example of this control.
Treeview control is a navigation control in ASP.NET and it displays data in the form of a tree structure. In Treeview control we can add hierarchical data through built in wizard.
In below example we are using hard coded path which you can change it to dynamic if your project demands that.
We will see step by step implementation of Treeview below,
First you have to drag and drop three controls, one tree view control and two button control to expand and collapse the treeview.
You may use Treeview.ExpandAll() method to expand all the nodes of your tree view, similarly to collapse the treeview you may use TreeView.CollapseAll() method.
Since we have to loop through the Directory You have to add the System.IO namespace.
In below method what I have done is, first loop through the folders to find the parent node and then through the files inside the folders to find the child node.
.aspx page to design the Treeview page is simple and will looks like below,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="treeview.aspx.cs"
Inherits="Practice2010.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnExpand" runat="server" onclick="Button1_Click"
Text="Expand Nodes" />
<asp:Button ID="BtnCollapse" runat="server" onclick="BtnCollapse_Click"
Text="Collapse Nodes" />
<asp:TreeView ID="tvFolderView" runat="server" ExpandDepth="0"
onselectednodechanged="tvFolderView_SelectedNodeChanged">
</asp:TreeView>
</div>
</form>
</body>
</html>
Below is the complete code to create the ASP.NET Treeview control:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace Practice2010
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateTreeView();
}
}
private void CreateTreeView()
{
DirectoryInfo parentDir = new DirectoryInfo("D:\\Song");
TreeNode parentNode = new TreeNode(parentDir.Name, parentDir.FullName);
tvFolderView.Nodes.Add(parentNode);
CreateTree(parentDir, parentNode);
}
private void CreateTree(DirectoryInfo RootDir, TreeNode MyNode)
{
foreach (DirectoryInfo dir in RootDir.GetDirectories())
{
TreeNode objNode = new TreeNode(dir.Name, dir.FullName);
MyNode.ChildNodes.Add(objNode);
CreateTree(dir, objNode);
}
foreach (FileInfo File in RootDir.GetFiles())
{
TreeNode Filenode = new TreeNode(File.Name, File.FullName);
MyNode.ChildNodes.Add(Filenode);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
tvFolderView.ExpandAll();
}
protected void tvFolderView_SelectedNodeChanged(object sender, EventArgs e)
{
}
protected void BtnCollapse_Click(object sender, EventArgs e)
{
tvFolderView.CollapseAll();
}
}
}
Final output will looks like below,
You are always welcome to ask questions if you have any to me below.
How can we drag & drop folder from one folder to another in asp.net using treeview
Plz Its Urgent