Description:-
in this example we explain
that how to bind directory and subdirectory or file in TreeView control or
display Directory Structure in asp.net.
I had the need for a web-application
that could use TreeView control in order to present a Directory folder and file
structure similar to Windows Explorer.
Here I was Defined the First Root
Directory and create a TreeNode and in which we will bind the all subdirectory
of the Root directory.
if you have to display All Files in Hierchy of the Directory then you have to put another Foreach Loop for the GetFiles like
System.IO.FileInfo[] Files = directory.GetFiles();
for (int FileCount = 0; FileCount < Files.Length; FileCount++)
{
DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));
}
you can also add and remove Directory dynamically in Treeview based Hierchy. we can also provide a checkboxes with each and every TreeNode so user can easily navigate to the particular directory and perfomed a operation like Add or Remove Directory under the particular root Directory.
to sorting data in gridview with up and down arrow sorting data in gridview in asp.net
change the background of gridview row when mouse is hover changebackground color of gridview in asp.net
treehierchy.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="treehierchy.aspx.cs" Inherits="treehierchy"
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server"><div><asp:TreeView Id="Roottreeview"PathSeparator = "|"ExpandDepth="0"runat="server" ImageSet="Arrows"AutoGenerateDataBindings="False" BackColor="#FFFFCC" BorderColor="Black" ShowCheckBoxes="All"><SelectedNodeStyle Font-Underline="True"HorizontalPadding="0px" VerticalPadding="0px" ForeColor="#5555DD"></SelectedNodeStyle><NodeStyle VerticalPadding="0px" Font-Names="Tahoma" Font-Size="10pt"HorizontalPadding="5px" ForeColor="#000000" NodeSpacing="0px"></NodeStyle><ParentNodeStyle Font-Bold="False" /><HoverNodeStyle Font-Underline="True" ForeColor="#5555DD"></HoverNodeStyle></asp:TreeView>
</div>
</form>
</body>
</html>
treehierchy.aspx.cs:-
using
System;
using
System.Collections.Generic;
using
System.Configuration;
using
System.Data;
using
System.IO;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Media;
using
System.Drawing;
using
System.Drawing.Imaging;
public partial class treehierchy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
if
(!Page.IsPostBack)
{
System.IO.DirectoryInfo Root = new System.IO.DirectoryInfo(Server.MapPath("~/"));
// output the directory into a
node
TreeNode RootNode = Directorystructure(Root, null);
// add the output to the
tree
Roottreeview.Nodes.Add(RootNode);
}
}
TreeNode
Directorystructure(System.IO.DirectoryInfo directory, TreeNode
parentNode)
{
// validate param
if (directory == null) return null;
// create a node for this
directory
TreeNode DirNode = new TreeNode(directory.Name);
// get subdirectories of the current
directory
System.IO.DirectoryInfo[]
childdirectory = directory.GetDirectories();
// OutputDirectory(SubDirectories[0],
"Directories");
// output
each subdirectoryfor (int dircount = 0; dircount <
childdirectory.Length; dircount++)
{
Directorystructure(childdirectory[dircount],
DirNode);
}
// output the current directories
file
System.IO.FileInfo[] Files =
directory.GetFiles();
for (int FileCount = 0; FileCount <
Files.Length; FileCount++)
{
DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));
} // if the parent
node is null, return this node
// otherwise add this node to the parent and return the
parentif (parentNode == null)
{
return
DirNode;
}
else
{
parentNode.ChildNodes.Add(DirNode);
return
parentNode;
}
}
}
0 comments:
Post a Comment