Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > TreeNodePopulate - InvalidCastException

Reply
Thread Tools

TreeNodePopulate - InvalidCastException

 
 
woker@gmx.net
Guest
Posts: n/a
 
      11-21-2006
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>

 
Reply With Quote
 
 
 
 
westonhankins@gmail.com
Guest
Posts: n/a
 
      11-27-2006
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[i]);
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


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Treeview node parent is becoming null in TreeNodePopulate Alex Diaz ASP .Net 0 08-06-2010 02:12 PM
TreeView SelectedNodeChanged Triggers Parent's TreeNodePopulate? dmeglio@gmail.com ASP .Net 1 08-04-2008 09:55 PM
Redirecting in 2.0 TreeView TreeNodePopulate =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?= ASP .Net 0 07-05-2006 07:56 PM
User Control - InvalidCastException: Specified cast is not valid Ajit ASP .Net 1 04-24-2004 09:28 PM
System.InvalidCastException: Specified cast is not valid Floela ASP .Net 3 07-10-2003 10:36 AM



Advertisments