tags:

views:

1145

answers:

2

I have a class that inherits from TreeNode, called ExtendedTreeNode. To add an object of this type to the treeview is not a problem. But how do I retrieve the object from the treeview?

I have tried this:

TreeNode node = tvManual.Find("path/to/node"); // tvManual is a treeview

return ((ExtendedTreeNode)node).Property;

But this doesn't work. I get this error: Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to type 'PCK_Web_new.Classes.ExtendedTreeNode'.

What do I have to do to make this work?

------------ SOLUTION -----------------

[Edit] My custom TreeNode class looks like this:

public class ExtendedTreeNode : TreeNode
{
    private int _UniqueId;

    public int UniqueId
    {
        set { _UniqueId = value; }
        get { return _UniqueId; }
    }
    public ExtendedTreeNode()
    {
    }
}

And this way I add Nodes to my treeview:

ExtendedTreeNode TN2 = new ExtendedTreeNode();

TN2.Text = "<span class='Node'>" + doc.Title + "</span>";
TN2.Value = doc.ID.ToString();
TN2.NavigateUrl = "ViewDocument.aspx?id=" + doc.ID + "&doc=general&p=" + parent;
TN2.ImageUrl = "Graphics/algDoc.png";
TN2.ToolTip = doc.Title;
TN2.UniqueId = counter;

tvManual.FindNode(parent).ChildNodes.Add(TN2);

And this way I retrieve my ExtendedTreeNode object:

TreeNode node = tvManual.Find("path/to/node");
ExtendedTreeNode extNode = node as ExtendedTreeNode;
return extNode.UniqueId;

I am using .NET 3.5 SP1

+1  A: 

You could try the following to get all nodes under your parent:

TreeNode[] parentNode = treeView1.Nodes.Find (parentid, true);
foreach(TreeNode node in parentNode)
{
    ExtendedTreeNode ext_tree_node = node as ExtendedTreeNode;
    if(ext_tree_node != null)
    {
        // Do your work
    }
}
SwDevMan81
When I do ExtendedTreeNode enode = node as ExtendedTreeNode; enode is always null. How come?
Martijn
when using the as casting (called safe casting), if the casting fails, the object is set to null
astander
How do I get this to work? Or what is going wrong?
Martijn
Ok, I just looked at how you are adding them. Youll want to change my example from "node as ExtendedTreeNode" to "node.Tag as ExtendedTreeNode". Which brings me to my question, why are you doing that? I think you want to create NT2 and use that (i.e, do not create "TreeNode tn = new TreeNode();"). Then add TN2, not tn, and my original post should work.
SwDevMan81
And your ExtendedTreeNode class should start with the following: "public class ExtendedTreeNode : TreeNode"
SwDevMan81
A: 

I assume you're creating the nodes as ExtendedTreeNodes.

I've noticed that the XxxView (ListView, TreeView, DataGridView) controls tend to clone things unexpectedly. Have you implemented ICloneable.Clone ()?

That may work; TreeNode implements Clone () virtually.

I find it easier, though to implement extended properties using the Tag property of the treenode:

TreeNode node = tvManual.Find("path/to/node");
return node.Tag as ExtendedTreeNode;


I strongly recommend against using Clone (); it's a fragile pattern. Use the Tag property:

class ExtendedInfo
{
    string NavigateUrl;
    string ImageUrl;
    int UniqueId;

    // other custom properties go here
}

// ...

void CreateTreeNode ()
{
    TreeNode TN = new TreeNode();

    string parent = "parent";    

    TN.Text = "<span class='Node'>" + doc.Title + "</span>";
    TN.Value = doc.ID.ToString();
    TN.ToolTip = doc.Title;

    ExtendedInfo extInfo = new ExtendedInfo;
    extInfo.NavigateUrl = "ViewDocument.aspx?id=" + doc.ID + "&doc=general&p=" + parent;
    extInfo.ImageUrl = "Graphics/algDoc.png";
    extInfo.UniqueId = counter;

    TN.Tag = extInfo;
}

// ...

ExtendedInfo GetExtendedInfo (TreeNode node)
{
    return node.Tag as ExtendedInfo;
}
XXXXX
No, I don't have implmented ICloneble.Clone() Should I do this? If so, how?
Martijn
The Tag property of the TreeNode doesn't exist?
Martijn
object TreeNode.Tag does exist in Windows Forms. Not sure about ASP.NET, but why wouldn't it?
XXXXX
I don't know, but I get an error if I use node.Tag
Martijn
Assuming you have a reasonable copy constructor: public override object Clone () { return new ExtendedTreeNode (this); }However, Clone () is a very fragile pattern. I strongly recommend looking harder for the .Tag () property.
XXXXX
Show me your code... What version of .NET are you using? I'm on 3.5 SP1; I don't have docs for 2.0.
XXXXX
And how (/when) do I call the Clone property?
Martijn
I will edit my post. How does the copy constructor looks like?
Martijn
The TreeView will call clone. I strongly suspect it's already cloning its nodes, dropping the ExtendedTreeNode part.
XXXXX
Copy constructors are OOP 101. MyClass (MyClass original) { this.field1 = source.field1; ... }Remember to copy the TreeNode fields as well.But really: look harder for the .Tag property. It's definitely in 2.0. What error are you getting?Remember: you have to add the tag when you create the node:TreeNode node = new TreeNode (...);ExtendedTreeNode ext = new ExtendedTreeNode (...);...node.Tag = node;
XXXXX
Okay, I Have edited my startpost. I am using .NET 3.5 SP1
Martijn
'Remember to copy the TreeNode fields as well' So I have to override the properties in my ExtendedTreeNode class? Error .Tag: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Martijn
I added more info in my answer.
XXXXX
Thnx, but the Tag property doesn't exists :-(
Martijn
I've also noticed, you're not inheriting from TreeNode?
Martijn
No: The extended info doesn't need to inherit from TreeNode: It's contained by a TreeNode.In your original question, post the code using the Tag property that gives you an error
XXXXX
I have edited the code in my post
Martijn
Look in the code I added to my answer as to how to use the Tag property. I have to go. Good luck.
XXXXX
Thnx for yhe help, but it didn't work. I still getting the same error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Martijn