无法将类型为System.Web.UI.WebControls.TreeNode的对象强制转换为“自定义"节点 [英] Unable to cast an object of type System.Web.UI.WebControls.TreeNode to Custom node
问题描述
我有一个自定义类"Node",它继承了System.Web.UI.WebControls.TreeNode类.在树视图的SelectedNodeChanged事件上,我试图将Treeview.SelectedNode的类型更改为自定义类型Node.在执行此类型转换时,我收到了InvalidCastException.
********************编辑**********************
SelectedNodeChanged事件HANDLER的定义如下
Hi,
I have a custom class ''Node'' inheriting the System.Web.UI.WebControls.TreeNode class. On the SelectedNodeChanged event of the treeview, I am trying to change the type of Treeview.SelectedNode to the custom type, Node. While performing this type casting I am getting the InvalidCastException.
********************EDIT**********************
The SelectedNodeChanged event HANDLER is defined as follows
protected void tvTreeview_SelectedNodeChanged(object sender, EventArgs e)<br />
{<br />
TreeNode tn = tvTreeview.SelectedNode;<br />
Node selectednode = (Node)tn; //Exception thrown at this line<br />
}<br />
**************************************************
由于"e"的类型为EventArgs,因此没有为其定义任何属性.
有人可以帮我解决这个问题吗?
紧急!
**********************************************
Since ''e'' is of type EventArgs, there are no properties defined for it.
Can anybody please help me with this problem?
Its urgent!!
推荐答案
您可能需要将sender
强制转换为适当的类型.
编辑==================
您仍然必须强制发送者来获取选定的节点:
You probably need to cast thesender
to the appropriate type instead.
EDIT ==================
You still have to cast the sender to get the selected node:
Treeview tree = sender as TreeView;
Node node = tree.SelectedNode as Node;
没办法!您不能将对TreeNode
对象的引用类型转换为Node
对象.您只能强制转换引用Node
对象的TreeNode
引用.例如:
No Way! You cant typecast a reference to aTreeNode
object to aNode
object. You can only typecast aTreeNode
reference that refers to aNode
object. For Example:
TreeNode tn = new Node();
Node n = (Node) tn;
因此,请确保tn
引用Node
对象.
问候.
So make sure that tn
refers to a Node
object.
Regards.
这篇关于无法将类型为System.Web.UI.WebControls.TreeNode的对象强制转换为“自定义"节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!