views:

1636

answers:

2

Hi,

I have a treeview in my masterpage. When a contentpage is loaded i want to save the treeview state (which nodes are collapsed/expanded). I store the nodes in an ArrayList. Code:

private void SaveTreeviewState(TreeNodeCollection nodes)
{
    foreach (TreeNode t in nodes)
    {
        // Store expandable state in ArrayList (true or false)
        //NodePaths.Add(t.Expanded);
        NodePaths.Add(t);

        // Check for childnods
        if (t.ChildNodes.Count > 0)
            // recall this method
            SaveTreeviewState(t.ChildNodes);

    }
}

This method is called by the unload event of the treeview object:

protected void tvManual_Unload(object sender, EventArgs e)
{
    SaveTreeviewState(tvManual.Nodes);

    // Clear session
    Session["Treeview"] = null;

    // Add arraylistm to session
    Session["Treeview"] = NodePaths;

}

In the load event of the masterpage i check whether my Session is set. When the session is set i call the method which read the session.

The arraylist in the session contains all my nodes, so that's correct. However, all nodes have the property expended set to false. This isn't correct because i expanded mupliple nodes.

Hope you guys understand my problem and can help me out.

Thnx in advanced

+1  A: 

Because the saved list is actually a list of objects (TreeNode objects), you are actually storing references to the objects. I am guessing that on tvManual_Unload the expanded state is changing or something similar. Probably you are using inprocess session which is similar to having a reference to the objects (there is no serialization) so any change to the object properties are also visible to the objects stored in session.

You could avoid such side effect by storing values into the session. For example store a Dictionary<string, bool> where the key will contain the node path and the value will contain the expanded state.

Aleris
I now store the values instead of the objects: NodePaths.Add(t.Expanded);It didn't help. All the valules are false
Martijn
Are you sure the values are correct when you add them with NodePaths.Add(t.Expanded)? It just doesn't make sense that once stored on session to change if they are value types.
Aleris
What do you mean with the correct values? When i expand treenodes on my masterpage, i know that expanded must be true.
Martijn
But if you print (or see them in debugger) all the values when you actually store them with NodePaths.Add(t.Expanded) are they correct? (eg: true for expanded as you expect?)
Aleris
No, that's my problem. By default my treeview is collapsed. When i expand some nodes, and the unload event is triggered, all the nodes have the property expanded set to false. So my array NodePaths contains only false values...
Martijn
The the problem has nothing to do with the session storing is all about saving the right values in the right moment. You should try to save the state in an earlier event (on load?).
Aleris
i don't think onload is an option. Imagine the page is loaded. You expand some nodes and than click on a menu-item to load page B (which is a content page). So i can't store the state in onload, because when page B is loaded, the treeview state on the masterpage is gone
Martijn
If there is no postback to the page that contain the tree view there is no way to know on server side what items are expanded on the client. The tvManual_Unload is hapening the first time the page is displayed, then the user expand some nodes then, if he just navigate away the state is lost.
Aleris
You basically have two options: 1) ensure the page is posted back each time (no navigation links) and save the state on postback 2) use ajax (with an update panel for example) so that when the tree is expanded it goes to the server and you have the chance to save the state
Aleris
When is the unload event actually fired? I think i have the wrong idea about when the unload event is triggered..
Martijn
Search for" ASP.NET Page Life Cycle". All events are happening on the server, before the page is displayed on the client. When a new postback occur all events are again fired.
Aleris
I'll search for it. I just found out this: When the SelectedNodeChanged is triggered then the load sequence is: page_load, selectednodechanged, treeview_unload. Why is page_load (masterpage) triggered? And i can't find anything when the unload of the treeview is fired, when is it fired?
Martijn
+1  A: 

Haven't tried implementing this myself but This guy seems to have worked it out...

Jason Punyon
I am beware of that site, but i wonder why my code doesn't work
Martijn