Welcome Guest Search | Active Topics | Sign In | Register

Can't downcast EO.Web.TreeNode to myNode Options
Filipe Barrancos
Posted: Tuesday, June 4, 2013 4:58:29 AM
Rank: Newbie
Groups: Member

Joined: 6/4/2013
Posts: 3
Hello all. I've been struggling for a few days on this, and can't seem to make it. It's probably a newbie question, but I can't figure it out.

Both my TreeView and Nodes are subclassed. At runtime, I'm adding new myNode() to each Node.Children. Then I have the OnItemPopulate event.

The declaration of myNode:
public class myNode : EO.Web.TreeNode

The problem is that (for some nodes) the EventArgs carries a EO.Web.TreeNode and I can't cast it to myNode, although I know it was originally created as a myNode. It gives me a runtime error invalidcastexception.

So, EventArgs's TreeNode argument is not of type myNode:
EO.Web.TreeNode thisNode is myNode = false

But I did create it and add it when it was a myNode! What am I missing?
eo_support
Posted: Tuesday, June 4, 2013 9:24:19 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

You can't sub class TreeNode. You can create your own myNode object when the page loads, however that object is discarded as long as the page is rendered. When the page posts back again, the TreeView will try to recreate the whole tree based on information saved in view state. During that phase it will only create TreeNode because it does not know anything about your myNode.

Thanks!
Filipe Barrancos
Posted: Tuesday, June 4, 2013 9:43:51 AM
Rank: Newbie
Groups: Member

Joined: 6/4/2013
Posts: 3
Thanks for the reply. I understand, but I wonder why it worked when my tree object was a subclass of TreeView instead of EO.Web.TreeView.

Still, being this the case, I have a newbie problem that must be easy to solve. If I dynamically create children (on click of parent), and those children have to be of type myNode (to store information I gather at creation time), how to I access it afterwards?

So, the OnClick event on a child sends me a TreeNode, how to I access information stored in myNode? Maybe I should re-create all myNodes in every postback (BD queries all over again)? Where/how - can you help me?
eo_support
Posted: Tuesday, June 4, 2013 9:47:23 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

You need to forget about subclassing TreeNode. It won't work. Period. The reason that it works for TreeView is because the TreeView object in this case is not dynamically created.

To solve your problem, you can use TreeNode.Value property to store a key information. Then use that key to look up whatever other information you wanted to associate with the TreeNode.

Thanks!
Filipe Barrancos
Posted: Tuesday, June 4, 2013 1:57:01 PM
Rank: Newbie
Groups: Member

Joined: 6/4/2013
Posts: 3
Ok, I don't have that choice because my object is too complex... it involves a lot of variables and functions, i.e., a class.

So this means I can't have an OO approach to web building? What would you say is the correct practice to maintaining objects instantiated?

If I don't use EO.Web it works... can you extend on your comment "TreeView object in this case is not dynamically created"?

Sorry for this basic questions, but your product seems really good and this is a deal breaker... I won't be able to use it.
eo_support
Posted: Tuesday, June 4, 2013 3:55:05 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,195
Hi,

It's not about OO aproach or not. You are asking for things that do not exist with ASP.NET. Specifically, you are asking dynamically created objects of your own types to be saved and restored across round trips. It's fine that your object is complex and has many variable and functions, but you have to keep your data object and the TreeNode object separate. Your data object is your object, the TreeNode is TreeNode. You can not inherit from the TreeNode object and attach your data to it in the hope that it will be correctly saved and restored. This is because ASP.NET knows how to save and restore member data for a TreeNode object (because we wrote to code to save and restore everything). But ASP.NET does not know how to save and restore your myNode object.

Your only option is to rebuild your data object every time, or save and restore them yourself. In either case, the first thing you do is to separate your data object and the TreeNode object instead of thinking that you can merge them together with a derived class. Once you have your data object separated from the TreeNode, the next question is about how to associate them together. This is why we were telling you to use the TreeNode.Value property to store a key information and then use that value to associated to your data object. For example, if each TreeNode represents an Employee, then you can store EmployeeID in TreeNode.Value property. Then use whatever way you have to rebuild your Employee object based on the EmployeeID provided by the TreeNode.

Hope this clears it up for you.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.