How do I inherit from the treenode class?
I have a class that inherits from TreeNode called ExtendedTreeNode. To add an object of this type to the tree is not a problem. But how do you get an object from a tree?
I've tried this:
TreeNode node = tvManual.Find("path/to/node"); // tvManual is a treeview
return ((ExtendedTreeNode)node).Property;
But it doesn't work. I am getting this error: Unable to pass object of type "System.Web.UI.WebControls.TreeNode" for input "PCK_Web_new.Classes.ExtendedTreeNode".
What do I need to do to make this work?
------------ DECISION -----------------
[Edit] My own 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 tree view:
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 get 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
source to share
I found a Microsoft example for a TreeView
tagged ASP.NET implementation here .
Some important steps that are needed beyond just subclassing TreeNode
and adding a property Tag
:
You must subclass TreeView
and override the method CreateNode
:
public sealed class TaggedTreeView : TreeView {
protected override TreeNode CreateNode() {
return new TaggedTreeNode(this, false);
}
}
This prevents TreeView
your new nodes from being overwritten with TreeNode
your extended node type instead.
You will need to have this particular constructor. (It looks like it works with the default constructor too, but there's a good reason why they used this one.)
public sealed class TaggedTreeNode : TreeNode {
object _Tag;
public object Tag { get { return _Tag; } set { _Tag = value; } }
...
protected internal TaggedTreeNode(TreeView owner, bool isRoot)
: base(owner, isRoot) {
}
To use extended TreeView
, you need a line Register
as indicated in your markup to import like:
<%@ Register TagPrefix="asp" Namespace="MyNamespace" Assembly="MyAssembly" %>
...
<asp:TaggedTreeView ID="taggedTreeView" runat="server" />
You should also override LoadViewState
and SaveViewState
in your inherited class TreeNode
:
public sealed class TaggedTreeNode : TreeNode {
...
protected override void LoadViewState(object state) {
var arrState = (object[])state;
_Tag = arrState[0];
base.LoadViewState(arrState[1]);
}
protected override object SaveViewState() {
var arrState = new object[2];
arrState[1] = base.SaveViewState();
arrState[0] = _Tag;
return arrState;
}
This allows the TreeView
value of your properties to persist Tag
between postbacks.
source to share
I am assuming you are creating nodes as ExtendedTreeNode
s.
I noticed that XxxView controls (ListView, TreeView, DataGridView) usually clone things unexpectedly. Have you implemented ICloneable.Clone ()
?
It might work; TreeNode actually implements Clone ()
.
It gets easier for me, although the Tag property treenode is used to implement the extended properties:
TreeNode node = tvManual.Find("path/to/node");
return node.Tag as ExtendedTreeNode;
I highly recommend using Clone ()
; it is a fragile picture. 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;
}
source to share