Extended TreeNode

  • Thread starter Thread starter Hakan Ugur
  • Start date Start date
H

Hakan Ugur

Hi All,
I got a treeview object. I extended its TreeNodes to ExTreeNode and then
added ExTreeNode to Treeview control and binded to screen. And then, when I
try to read treeview it returns TreeNode instead of ExTreeNode. When I try
to cast to ExTreeNode, it throws Invalid Cast Exception.

Have you got any idea?

Thanks.
 
Hi All,
I got a treeview object. I extended its TreeNodes to ExTreeNode and then
added ExTreeNode to Treeview control and binded to screen. And then,
when I
try to read treeview it returns TreeNode instead of ExTreeNode. When I
try
to cast to ExTreeNode, it throws Invalid Cast Exception.

Have you got any idea?

Yes. Clearly whatever ExTreeNode instance you've created, that's not
what's in the TreeView where you "read" it.

Beyond that, without you posting a concise-but-complete sample of code
that reliably reproduces the problem, there's no way to know for sure what
you've done wrong.

Pete
 
Ok, here the code.

public class ExtendedTreeNode:TreeNode
{
private DateTime _CreationDate;

public DateTime CreationDate{
get{
return _CreationDate;
}set{
_CreationDate=value;
}

}
}

And this is WebForm which is contains TreeView;

public partial class Default: System.Web.UI.MasterPage{
private void FillTreeView{
ExtendedTreeNode exTreeNode=new ExtendedTreeNode();
exTreeNode.CreationDate=DateTime.Now;

TreeView1.Nodes.Add(exTreeNode);
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs
e){
// It is throwing exception in this line.
(ExtendedTreeNode)TreeView1.SelectedNode;
}
}
 
Ok, here the code.

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
 
Back
Top