|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
TreeNodePopulate - InvalidCastExceptionmy 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> 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 wo***@gmx.net wrote: Show quoteHide quote > 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>
GridView DDL and Label
hyperlink in a ListItem Getting the generated name attribute for use in JavaScript Microsoft ASP.NET 2.0 Treeview not being W3C compliant? Gridview -- changing field content zorder problem with menu and combobox hyperlink on template header gridView Viewstate Control Position Panel Border Problems |
|||||||||||||||||||||||