W
woker
Hi group,
my problem is the following:
I derive a class from System.Web.UI.WebControls.TreeNode, add a node of
my derived class to TreeView and on the TreeNodePopulate callback (or
any other) try to downcast the received node into my derived class. I
get an Invalid Cast Exception.
The following is a sample code that demonstrates my problem:
using System;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.Nodes.Add(new PineNode());
}
public class PineNode : TreeNode
{
public PineNode(): base()
{
this.SelectAction = TreeNodeSelectAction.Expand;
this.PopulateOnDemand = true;
this.Text = "I am type: " + this.GetType().ToString();
}
}
protected void TreeView1_TreeNodePopulate(object sender,
TreeNodeEventArgs e)
{
PineNode myNode = (PineNode)e.Node;
// InvalidCastException! Unable to cast object of type
// System.Web.UI.WebControls.TreeNode to type PineNode.
}
}
And the page is defined as follows:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CowNode becomes TreeNode</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server"
ExpandDepth="0"
OnTreeNodePopulate="TreeView1_TreeNodePopulate">
</asp:TreeView>
</div>
</form>
</body>
</html>
my problem is the following:
I derive a class from System.Web.UI.WebControls.TreeNode, add a node of
my derived class to TreeView and on the TreeNodePopulate callback (or
any other) try to downcast the received node into my derived class. I
get an Invalid Cast Exception.
The following is a sample code that demonstrates my problem:
using System;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.Nodes.Add(new PineNode());
}
public class PineNode : TreeNode
{
public PineNode(): base()
{
this.SelectAction = TreeNodeSelectAction.Expand;
this.PopulateOnDemand = true;
this.Text = "I am type: " + this.GetType().ToString();
}
}
protected void TreeView1_TreeNodePopulate(object sender,
TreeNodeEventArgs e)
{
PineNode myNode = (PineNode)e.Node;
// InvalidCastException! Unable to cast object of type
// System.Web.UI.WebControls.TreeNode to type PineNode.
}
}
And the page is defined as follows:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CowNode becomes TreeNode</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server"
ExpandDepth="0"
OnTreeNodePopulate="TreeView1_TreeNodePopulate">
</asp:TreeView>
</div>
</form>
</body>
</html>