How to Create Treeview from Folder in Asp.Net


Description:-

In this article we will see how to create treeview control from folder path. We can bind treeview control from server folder path in dot net. Here I have used vehicle folder to see some levels so you can see easy to understand and bind your own folder in treeview control.

Default.aspx:-

<div>
  <h3>Vehicle Details</h3>
  <hr />
  <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
    <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
    <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
    <ParentNodeStyle Font-Bold="False" />
    <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px" VerticalPadding="0px" />
  </asp:TreeView>
</div>

Create Folder and Some levels of that Folder so we can create Treeview from that folder in your Web Application.

Now Go to Code behind file and Bind Folder using Dictionary Info through Folder and Create Treeview. Here you can see I have Called in page load event so when page call first time then Find Folder and Check All levels from that Folder.

Default.aspx.cs:-

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
    DirectoryInfo rootInfo = new DirectoryInfo(Server.MapPath("~/Vehicles/"));
    this.PopulateTreeView(rootInfo, null);
  }
}

Now PopulateTreeview from Getting Data and Read all Nodes from That Folder and Bind it in Treeview Control. Here I have Used How to Create Levels in Treeview Control.

private void PopulateTreeView(DirectoryInfo dirInfo, TreeNode treeNode)
{
  foreach (DirectoryInfo directory in dirInfo.GetDirectories())
  {
    TreeNode directoryNode = new TreeNode
    {
      Text = directory.Name,
      Value = directory.FullName
    };

    if (treeNode == null)
    {
      //If Root Node, add to TreeView.
      TreeView1.Nodes.Add(directoryNode);
    }
    else
    {
      //If Child Node, add to Parent Node.
      treeNode.ChildNodes.Add(directoryNode);
    }

    //Get all files in the Directory.
    foreach (FileInfo file in directory.GetFiles())
    {
      //Add each file as Child Node.
      TreeNode fileNode = new TreeNode
      {
        Text = file.Name,
        Value = file.FullName,
        Target = "_blank",
        NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName)).ToString()
      };
      directoryNode.ChildNodes.Add(fileNode);
    }

    PopulateTreeView(directory, directoryNode);
  }
}

now run your Application in Browser and there you can see treeview with your Specified Folder path. In my case look like below image.

Related Posts

Previous
Next Post »

5 comments

comments
Anonymous
September 9, 2018 at 3:27:00 PM GMT+5:30 delete

I am not sure wheгe you're gеtting your information,
but great topic. Ӏ needѕ to spеnd some time learning more or underѕtanding morе.
Thanks for ѡonderful information Ӏ ᴡas looking for this information for my
mission.
ѡhy not trү these out : How To Passwoгⅾ Protect
Foldeг In Less Than 9 Minutes Using These Amazing Tօols

Reply
avatar
Anonymous
September 14, 2018 at 12:39:00 AM GMT+5:30 delete

I ԛսite liкe looking through an article that can make people
think. Also, thanks for permitting me tօ comment!

Source : How To Lock Files When Nobody Else Will

Reply
avatar
Anonymous
April 14, 2019 at 4:22:00 AM GMT+5:30 delete

I'm amazed, I have to admit. Seldom do I come
across a blog that's both educative and amusing, and without a doubt, you have hit the nail on the head.
The issue is something which too few men and women are speaking intelligently about.

I am very happy that I came across this during my search for
something relating to this.

Reply
avatar
Anonymous
April 17, 2019 at 6:57:00 AM GMT+5:30 delete

I am really thankful to the holder of this web site who has shared this
great paragraph at here.

Reply
avatar
February 29, 2020 at 9:43:00 AM GMT+5:30 delete

Great..... Need how to add folder and files in treeview

Reply
avatar

Thanks for comments.....