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