How do you popular a Treeview with a bank list?

Asked

Viewed 872 times

1

I have a TreeView static representing folders and files in this template:

<asp:TreeView ID="TreeView1" runat="server">
<Nodes> 
    <asp:TreeNode Text="Diretorio1"  Target="_blank">
        <asp:TreeNode Text="arquivo1 "  Target="_blank"/>
        <asp:TreeNode Text="arquivo2"  Target="_blank"/>
        <asp:TreeNode Text="arquivo 3"  Target="_blank"/>
    </asp:TreeNode>
    <asp:TreeNode Text="diretorio2" NavigateUrl="~/Employer.aspx" Target="_blank">
        <asp:TreeNode Text="arquivo1" Target="_blank"/>
        <asp:TreeNode Text="arquivo2"  Target="_blank"/>
        <asp:TreeNode Text="arquivo3"  Target="_blank"/>
    </asp:TreeNode>
    <asp:TreeNode Text="Diretorio3" Target="_blank">
        <asp:TreeNode Text="arquivo1"  Target="_blank"/>
        <asp:TreeNode Text="arquivo2"  Target="_blank"/>
        <asp:TreeNode Text="arquivo3"  Target="_blank"/>
    </asp:TreeNode>
</Nodes>

And I have two methods in Entity Framework: One that returns a list of directories and another that returns list of files from a given directory. Both already work and I want to apply in my model.

How would I look to be popular this TreeView with these my methods?

  • Have you heard of JSTREE?

  • yes, but with the treeview in this model that is there.

  • Not long ago I used this one. Much better than treeviw http://answall.com/questions/43477/montar%C3%A1rvore-jstree

  • but I already use treeview, and already customized, but thanks anyway.

1 answer

1


Simple, just do a foreach on each your list, and create the Node via c#.

First, comment with the <% -%> the code of the nodes in your webforms, or remove leaving only the Treeview statement, as it will be dynamic now, leave that alone:

<asp:TreeView ID="TreeView1" runat="server">

Then on your pageLoad:

 var listDiretorios = metodoListarDiretorio();
 var listArquivos = metodoListarArquivos();
        foreach (var diretorio in listDiretorios)
        {      
            TreeNode node1 = new TreeNode(diretorio.nomeDiretorio);
            node1.NavigateUrl = "#"; //link 
            node1.PopulateOnDemand = false;
            node1.ImageUrl = "~/asstes/img/directory.png";
            TreeView1.Nodes.Add(node1);

            foreach (var arquivo in listArquivos)
            {
                TreeNode node2 = new TreeNode(arquivo.nomeArquivo);
                node2.PopulateOnDemand = true;
                node2.NavigateUrl = "#";
                node2.ImageUrl = "~/asstes/img/file.png";
                 //adiciona o node2 ao node1
                node.ChildNodes.Add(node2);
            }

        }

Basically it was only to create a list for the first Node (directories), then create one at the same time for the second Node (files), and add a Node in the other. NOTE: I have already set your icon of folders and files.

  • Worked out, perfectly worth here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.