How popular is a TreeView with a list of the bank?

1

I have a% 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 the Entity Framework: One that returns a list of directories and another that returns list of files of a certain directory. Both already work and I want to apply it to my model.

How would I get this TreeView popular with my methods?

    
asked by anonymous 13.02.2015 / 01:50

1 answer

1

Simple, just make a foreach on each list of yours, and create the Node via C #.

First, comment on <% -% > the code of the nodes in your webforms, or remove leaving only the declaration of TreeView, because it will be dynamic now, just leave it:

<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 just creating a list for the first node (directories), then creating one at the same time for the second node (files), and adding a node in the other. NOTE: I have already set up your folder and file icon.

    
13.02.2015 / 14:38