Restoring an Existing TreeView



I am using a TreeView on a web page ( 2.0) for navigation. When I
navigate to a different page, then return to the original page, I need to
have the TreeView reflect the current selected node. This is proving
difficult, since the SelectedNode property is read-only. (I previously posted
a question here to see if anyone knew of a way to programatically set the
SelectedNode, but there were no responses.)

In the June, 2006, issue of MSDN Magazine, Jeff Prosise had an article on
persisting TreeViews, in which he addressed the issue of the expansion state
of the nodes, but did not address the SelectedNode property. However, he
provided a hint, saying one could serialize the TreeView to Session then
deserialize it back, but he did not give a code example for this.

Accordingly I put the following code in the SelectedNodeChanged event handler:

Session["tvMenu"] = tvMenu;

where tvMenu is the TreeView declared, but not populated, in the .aspx file
of the page.

Then in Page_Load, I have the following lines of code to either build or
restore the TreeView:
if (Session["tvMenu"] != null)
tvMenu = Session["tvMenu"] as TreeView;
tvMenu_SelectedNodeChanged(null, null);
HelperStuff.BuildTreeViewMenu(ref tvMenu, strUserRole);

where HelperStuff.BuildTreeViewMenu is a helper method which gets the data
from the database and recursively builds up the Nodes collections. I am
confident that tvMenu is being correctly restored from Session because in the
debugger I can see that the Nodes collection and the SelectedNode property
have the expected values.

However, tvMenu is not displaying on the page. Looking at the page source
in IE, it is clear that it is not even being rendered. I have tried the
obvious things like setting the Enabled and Visible properties to true, but
to no avail.

Any suggestions?




Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question