Home All Groups Group Topic Archive Search About

Customizing a TreeNode's viewstate

Author
3 Jan 2006 3:38 PM
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?

Author
3 Jan 2006 4:20 PM
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!
Author
24 Jan 2006 8:56 PM
hohans
Keith Patrick wrote:
> 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!

Hi Keith,

I am having the same problem.  Would you mind posting your custom
TreeNode class and the changes you made that makes the whole thing
work?  Thanks a lot!