TreeNodePopulate - InvalidCastException

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>
 
W

westonhankins

Hi woker,

I also had this problem and there appears to be no solution from MS for
it (overriding CreateNode doesn't really help), but I'm currently using
the following workaround and haven't seen any terrible consequences.

Make a custom tree class that inherits from the TreeView control and
use this constructor:

1. public TrustTree()
2. {
3. this.EnableClientScript = false;
4. this.EnableViewState = false;
5.
6. TreeView treeState =
(TreeView)HttpContext.Current.Session["TrustTree"];
7. if( treeState != null )
8. {
9. for (int i = 0; i < treeState.Nodes.Count; i++)
10. {
11. this.Nodes.Add(treeState.Nodes);
12. }
13. }
14. else
15. {
16. this.Nodes.Add(new RootNode());
17. }
18. }

and this destructor:

100. public override void Dispose()
101. {
102. HttpContext.Current.Session.Add("TrustTree", this);
103. base.Dispose();
104. }

you must also activate sessions for this to work. What it is basically
doing is saving the whole tree on the server inside the session
variable (where it gets correctly serialized) and when you receive a
request from a client to do some operation on the tree you take your
locally saved copy of the tree and use that.

Everything else can be programmed normally inheriting from any
treenodes or childs of treenodes as you normally would.
TreeErrorEventArgs respects the node type now.

Hope this helps!
-Weston
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top