views:

190

answers:

1

Hi all,

I have a public folder on the server that contains recursively nested sub folders. In the various Leaf folders contains Images. I wanted to create a server side file browser that will display the Images to the user. I am using the ASP.NET TreeView Control. I create the tree nodes using PopulateOnDemand. If the user click on a leaf directory I want the images in that folder to be displayed in a DataList Control.

The problem is that when I click on a sub tree node (after I expanded it parent node) All the expanded sub tree disappears and only the parent node is showed with no + sign next to it !!

( I have set the TreeView's PopulateNodesFromClient property to true )

Can someone tell me what is the problem ??

Thanks

Here is the code :

<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False" 
                    onselectednodechanged="TreeView1_SelectedNodeChanged" 
                    ontreenodepopulate="TreeView1_TreeNodePopulate">
                </asp:TreeView>




protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string path = Server.MapPath(".");
        PopulateTopNodes(path);
    }
}

private void PopulateTopNodes(string pathToRootFolder)
{
    DirectoryInfo dirInfo = new DirectoryInfo(pathToRootFolder);
    DirectoryInfo[] dirs = dirInfo.GetDirectories();
    foreach (DirectoryInfo dir in dirs)
    {
        TreeNode folderNode = new TreeNode(dir.Name,dir.FullName);

        if (dir.GetDirectories().Length > 0)
        {
            folderNode.PopulateOnDemand = true;
            folderNode.Collapse();
        }
        TreeView1.Nodes.Add(folderNode);
    }
}


protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{

    if (IsCallback == true)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            LoadChildNode(e.Node);
        }
    }


}

private void LoadChildNode(TreeNode treeNode)
{
    DirectoryInfo dirInfo = new DirectoryInfo(treeNode.Value);
    DirectoryInfo[] dirs = dirInfo.GetDirectories();
    foreach (DirectoryInfo dir in dirs)
    {
        TreeNode folderNode = new TreeNode(dir.Name, dir.FullName);
        if(dir.GetDirectories().Length>0){
            folderNode.PopulateOnDemand = true;
            folderNode.Collapse();
        }
        treeNode.ChildNodes.Add(folderNode);
    }
}







protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    // Retrieve  the images here 
}
+1  A: 

I don't have a straight answer for you, but you are doing something wrong since you are only loading the nodes in Page_Load, and even only on the first load. That means that you rely on the ViewState to save all the nodes between postbacks and well, that's just not the correct way to do it.

It seems to me that you're pretty close, though. I would remove the Page_Load and then take a look at the last example of this page: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview(VS.80).aspx.

Jan Aagaard
Dear Jan ,Thank you for your answer.When the TreeView.PopulateNodesFromClient propertyis true (the default), the TreeView performs a client-side callback to retrieve the nodes it needs from A given node is populated on demand only once. After that, the values remain available on the client, and no callback is performed if the same node is collapsed and expanded.That's the way the control is supposed to behave.(Maybe using SelectedNodeChanged event together with PopulateNodesFromClient etc. is causing that bug ?)I will take a look on the example you recommend.Thank you
ProgNet
I took my example from [Here](http://www.aspnettutorials.com/tutorials/controls/treeview-open-files-cs.aspx)
ProgNet
To me, it seems that the difference between your code and the example is that all they do in Page_Load is set up the root node - everything else gets populated by the client calls. You're loading all the nodes in Page_Load.
Jan Aagaard
There is no problem with the way I first create and added the top level nodes to the tree in the PageLoad event handler.In the book 'ASP.NET 3.5 Unleashed' They used the same way that I did.That's not what is causing the problem.
ProgNet
Dear Jan ,Thank you for your help today :) I found what was causing the problem,the line :if (IsCallback == true)Once I comment out the line there was no problem of disappearing nodes
ProgNet
Nice to hear, that you found the solution. But I still dare you to check the size of the view state on your page. (Right click and check view source, and check the value of the <input type="hidden" name="__VIEWSTATE"/>. :)
Jan Aagaard
Thank you I will do that.In the description of the control it is written that when PopolateNodesFromClient property is set to true,the TreeView control preforms a client-side callback to retrieve the nodes it needs from server without posting back the entire page.But I know that for example in ASP Ajax there are problems with the ViewState size.So it will be a good thing to check that.
ProgNet