That's not a complete sample of code. Also, it appears that you're using
the .NET web forms, which is not exactly the same as regular forms
programming. You should always be clear about the exact classes, if
you're deviating from the standard Windows application environment.
If you do get around to posting complete code, hopefully someone will know
how to use it. I haven't used .NET for a web form application, and am not
sure I'd even know how to run code posted for something like that.
As far as debugging it goes, I would recommend you look at what the type
actually is, as well as the data in the instance. Obviously the type
isn't what you expected it to be, so the first thing to do is to confirm
whether the node instance you're trying to cast does in fact have any
relation to the one you added. There's nothing in the code you posted
that would guarantee that it does, never mind that it is exactly the node
you added.
If it is related to the one you added (e.g. it has some TreeNode field
that's unique and identical...you'd of course want to set some TreeNode
field so that you can test this), then the question becomes one of what's
going on with the node when you add it or after. Since I know this works
for a regular TreeView (I've used the same technique myself), it could be
something related to the web version of the classes you're using. What
that could be, I don't know since it's outside my experience.
Of course, it could just be a programming error on your part. Without a
complete sample of code (and instructions for how to use the program,
since the code apparently depends on the user action, being in the
SelectedNodeChanged event handler), there's no way to know.
Pete