TreeNodePopulate - InvalidCastException

Discussion in 'ASP .Net Web Controls' started by woker@gmx.net, Nov 21, 2006.

  1. Guest

    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>
     
    , Nov 21, 2006
    #1
    1. Advertising

  2. Guest

    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


    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>
     
    , Nov 27, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajit
    Replies:
    1
    Views:
    3,613
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  2. Floela
    Replies:
    3
    Views:
    575
    Jon Skeet
    Jul 10, 2003
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    Redirecting in 2.0 TreeView TreeNodePopulate

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    553
    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=
    Jul 5, 2006
  4. Replies:
    1
    Views:
    723
  5. Alex Diaz
    Replies:
    0
    Views:
    519
    Alex Diaz
    Aug 6, 2010
Loading...

Share This Page