Click here to Skip to main content
15,889,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display root nodes of tree view in asp .net page load and then after for each node click it will display data of that node.

When I click on first root it will display only first root data.
now when I click second root node It will display second root data hide first root data which i earlier click.

Also What ever root node I have selected I want display all files details in gridview
i.e file name, created date ,file path size ect.

Also want to display selected file data in text Editor.

What I have tried:

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


        <hr>
        <asp:GridView ID="GridView1" runat="server">
    </div>


Aspx.cs file code is as follows

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DirectoryInfo rootInfo = new DirectoryInfo(Server.MapPath("~/Vehicles/"));
                this.PopulateTreeView(rootInfo, null);
            }
        }

        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);
            }
        }

       

        protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            GetFilesAndFolders();
            //if (e.Node.Parent == null)
            //    return;
            //string strNodeValue = e.Node.Value;
            //foreach (TreeNode node in e.Node.Parent.ChildNodes)
            //{
            //    if (node.Value != strNodeValue)
            //        node.Collapse();
            //}
        }

        public void GetFilesAndFolders()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Vehicles"));
            FileInfo[] fileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
            GridView1.DataSource = fileInfo;
            GridView1.DataBind();
        }
    }


It Is Displaying all root node and all its child node on page load. Instead of that I want to display only All root nodes.
also it is displaying all files details of all child of all root on grid instead I want only selected root childs only
Posted
Updated 1-Nov-17 21:51pm
v3
Comments
Sibasisjena 2-Nov-17 3:47am    
After populating the treeview you can use the TreeNode.CollapseAll Method ()

1 solution

Please look into the solution provided in below link. I hope this would help you.
Collapse All Other Nodes in TreeView on Selection of One Node[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900