如何从树节点类继承? [英] How to inherit from the treenode class?
问题描述
我有一个类,从树节点继承,称为ExtendedTreeNode。要添加这种类型到TreeView的对象是没有问题的。但我怎么检索树形视图对象?
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?
我已经试过这样:
TreeNode node = tvManual.Find("path/to/node"); // tvManual is a treeview
return ((ExtendedTreeNode)node).Property;
但是,这并不正常工作。我得到这个错误:无法转换类型'System.Web.UI.WebControls.TreeNode对象键入'PCK_Web_new.Classes.ExtendedTreeNode。
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 -----------------
我的自定义树节点类看起来是这样的:
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);
和这样我取回我ExtendedTreeNode对象:
And this way I retrieve my ExtendedTreeNode object:
TreeNode node = tvManual.Find("path/to/node");
ExtendedTreeNode extNode = node as ExtendedTreeNode;
return extNode.UniqueId;
我使用.NET 3.5 SP1
I am using .NET 3.5 SP1
推荐答案
您可以尝试以下方法让你的父母下的所有节点:
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
}
}
这篇关于如何从树节点类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!