Save the state of a TreeView

F

Flyguy

I have a DataSource and a TreeView that I am rebinding. When I do this the
TreeView will close all of its nodes. Is there a way that I can save the
state of the TreeViews nodes so that I can reapply it after the rebinding
takes effect?

// Save the state of treeView
dataSource.DataBind();
treeView.DataBind();
//reapply the state so the treeView will be opened to the same node as it was.
 
S

Steven Cheng[MSFT]

Hi Flyguy,

From your description, you have ASP.NET web pages that use TreeView
control(bound to datasource), however, you find that whenever you rebind
the Treeview with the DataSource, the TreeNodes status(such as expand ,
collapse) will lost, you're looking for some means to keep such status,
correct?

As for the rebinding, how did it happen, is it due to the redirection
during page navigation? For such navigation case, you can consider use
separate frame pages to hold the TreeView navigation menu so as to avoid
reload/rebind on each redirection.

If the problem is due to other things, I think you can try the following
means for keep node status:

**ASP.NET TreeView control's "Nodes" property is a TreeNodeCollection, you
can copy it to an array and cache the array in memory(through cache or
session...)

** When page reload and TreeView rebind, you can loop through the cached
Node Array and assign the proper node status (for the corresponding nodes).

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
L

Louis Somers

Flyguy schreef:
I have a DataSource and a TreeView that I am rebinding. When I do this the
TreeView will close all of its nodes. Is there a way that I can save the
state of the TreeViews nodes so that I can reapply it after the rebinding
takes effect?

// Save the state of treeView
dataSource.DataBind();
treeView.DataBind();
//reapply the state so the treeView will be opened to the same node as it was.

DataBind basically throws the treeview internals away and creates a new one from the DataSet. This
way all treeview specific data such as expand-state is lost. To update the data without losing the
state, you will have to synchronize the treeview with your dataset yourself instead of using the
built in DataBind() function.

Synchronizing involves recursively iterating, removing old items and adding new ones. It helps if
the data is sorted, you may be able to take shortcuts if you know what has happened to the data
before synchronizing (items only removed, or only added e.g.)

Cheers,
Louis
 
L

LRuss

Hello Steve.

You say "you can consider use separate frame pages to hold the TreeView
navigation menu". Are you suggesting the use of framesets? As I understand
it, ASP.NET does not support framesets. Can you elaborate?

Thanks,
Len Russell
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top