a1jit Posted June 13, 2006 Posted June 13, 2006 (edited) Hi Guys, Im not so sure why this code throws out an casting error since this feature can be implemented in vs 2003 but not in vs express 2005.. Basically i have a class which is inheriting the TreeNode class public class DGTree : TreeNode { private string groupName; public DGTree() {} public DGTree(string groupName, string text) { this.GroupName = groupName; this.Text = text; this.NavigateUrl = "index.aspx"; this.PopulateOnDemand = true; } public string GroupName { get { return groupName; } set { groupName = value; } } } Then, somewhere in the aspx page, im using the above class to create a treenode DGTree parent = new DGTree("CAnalysis", "Fabrication"); TreeView1.Nodes.Add(parent); DGTree parent1 = new DGTree("CAnalysis", "Assembly"); TreeView1.Nodes.Add(parent1); Everything works fine..But when i try to retrieve the value of GroupName attached to each node using the event "TreeNodePopulate", i get some casting error. It seems like its not possible to cast the TreeNode Class to its subclass to retrieve its member values (GroupName) Error: Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to type 'DGTree'. protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) { DGTree node = (DGTree)e.Node; populateChildren(node.GroupName.ToString()); } Im not sure why, but this same approach works in the .net 2003, but no idea why it doesnt work in express edition 2005, am i missing something? Edited June 13, 2006 by a1jit Quote
Administrators PlausiblyDamp Posted June 13, 2006 Administrators Posted June 13, 2006 If you put a breakpoint on the line DGTree node = (DGTree)e.Node; what type of object does it think e.Node is? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
a1jit Posted June 13, 2006 Author Posted June 13, 2006 If you put a breakpoint on the line DGTree node = (DGTree)e.Node; what type of object does it think e.Node is? Its of type TreeNode..But in previous version, im able to do this kind of casting..because im just converting its type to its subclass which is the concept of OOP..Am i right..I dont think there is a syntax issue with this statement..What is your opinion on this..This type of casting is possible right? Quote
Cags Posted June 13, 2006 Posted June 13, 2006 I must admit I only have experience of .Net 1.1 and VS 2003, but as you say this type of conversion is certainly possible in those and I cannot see any conceptual reasons for them to have taken out such functionality. The only thing I can think of (and I highly doubt its your problem) is that there are two TreeNode objects in .Net, one in the System.Forms.Controls. Namespace, and the other is in the Web namespace as you say. Are you certain the DGTree object is inheriting from the right one? I would imagine if this was wrong you would encounter a problem before this point, but I just thought I'd throw it out there anyway. Quote Anybody looking for a graduate programmer (Midlands, England)?
HJB417 Posted June 13, 2006 Posted June 13, 2006 A random guess but seeing how you're accessing the treenode for reading on a postback.. it must reconstruct the treenode and maybe that's where the issue is... It's maybe using it's builtin classes to reconstruct the treenode. Quote
a1jit Posted June 16, 2006 Author Posted June 16, 2006 Hi Guys, thanks a lot for the reply, im not sure why the casting is still not working.. Actually this is wht im trying to accomplish. Im creating a few custom classes to represent each level of nodes..So im just trying to detect the type of node when the user try to expand the tree. So according to the type of node, then i know which value should be loaded as its child. I dont want to use the depth level as its very rigid, and its not good if there is a change need to be implemented in future. So i want to create something similar to the link shown below in asp.net. If anyone has some idea, hope you dont mind sharing it with me. Thanks al ot http://www.devcity.net/Articles/23/1/custom_treeview.aspx Quote
Gill Bates Posted July 4, 2006 Posted July 4, 2006 Actually, your problem is that you are trying to cast a TreeNode instance into a DGTree instance when the TreeNode instance is not really a DGTree instance. What I mean by this is that the TreeNode instance does not hold the signature of the DGTree class so, since there are no type converters defined, the CLR is going to throw an exception. Basically, DGTree is always a TreeNode but a TreeNode is not always a DGTree. If you try casting a TreeNode that is not a DGTree into a DGTree then you are going to get the error you experienced. TreeNode node = treeView1.SelectedNode; if (node is DGTree) { // both are valid DGTree downcast = (DGTree)node; TreeNode upcast = (TreeNode)downcast; } else { // invalid DGTree downcast = (DGTree)node; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.