Customizing a TreeNode's viewstate

K

Keith Patrick

I have a custom subclass of TreeNode that has to store it's own additional
viewstate to itself. However, the problem I run into is that even though
the owning TreeView is itself a subclass that overrides CreateNode(), then
calling TreeNode.LoadViewState(object), all ChildNodes are restored as
TreeNode rather than DerivedTreeNode, so when they themselves have their
viewstates restored, they are receiving modified viewstates (and basically,
my viewstate is new Object[] { base.SaveViewState(), <newViewStateInfo> })
not that haven't had the 2nd element stripped out and thus results in an
InvalidCastException as TreeNode.LoadViewState assumes the vs is of a
different type.
My question is: how can I override TreeNode.LoadViewState such that I can
restore the child node hierarchy with the instances being of the correct
subclass of TreeNode that knows how to handle the modified viewstate?
 
K

Keith Patrick

Found the problem after using Lutz Roeder's .Net Reflector to look at
TreeNode and TreeNodeCollection. Basically, TreeNodeCollection is the
problem, as it implements IStateManager, too. The problem was that my
subclassed TreeNode wasn't passing the Owner (TreeView) ctor parameter to
the base class, and TreeNodeCollection only calls CreateNode() (instead of
new TreeNode()) if the owner is not null. I have a separate Owner property,
so I wasn't noticing that a protected Owner property was null. Score 1 for
having access to a decompiler!
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top