Inherited TreeView Nodes and typeof
-
I am using an ASP.NET 2.0 TreeView and was wanting to create different child TreeNode types for the different items that I am loading into the treeview. The idea behind this is that all I should have to do is check the node type when a node gets clicked and I can then respond accordingly. The problem, however, is that the child treenode I created that inherits from TreeNode gets reported to be a normal TreeNode type rather than the inherited treenode type when I use the typeof operator even though I instantiate it as the child TreeNode type. Am I missing something?
class Child1TreeNode : TreeNode
{
}class Child2TreeNode : TreeNode
{
}And then in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Child1TreeNode node = new Child1TreeNode();
node.Text = "Hello World";
node.Value = "1";
Child2TreeNode node2 = new Child2TreeNode();
node2.Text = "Goodbye World";
node2.Value = "2";
TreeNode topNode = TreeView.FindNode("Top");
topNode.ChildNodes.Add( node );
}
}And then in the event handler for node changed:
protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = ((TreeView)sender).SelectedNode;
if (node.GetType() == typeof(Child1TreeNode) )
{
// Do something related to Child1TreeNode types
}
else if (node.GetType() == typeof(Child2TreeNode) )
{
// Do something related to Child2TreeNode types
}
}Thanks. -Matt
------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
-
I am using an ASP.NET 2.0 TreeView and was wanting to create different child TreeNode types for the different items that I am loading into the treeview. The idea behind this is that all I should have to do is check the node type when a node gets clicked and I can then respond accordingly. The problem, however, is that the child treenode I created that inherits from TreeNode gets reported to be a normal TreeNode type rather than the inherited treenode type when I use the typeof operator even though I instantiate it as the child TreeNode type. Am I missing something?
class Child1TreeNode : TreeNode
{
}class Child2TreeNode : TreeNode
{
}And then in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Child1TreeNode node = new Child1TreeNode();
node.Text = "Hello World";
node.Value = "1";
Child2TreeNode node2 = new Child2TreeNode();
node2.Text = "Goodbye World";
node2.Value = "2";
TreeNode topNode = TreeView.FindNode("Top");
topNode.ChildNodes.Add( node );
}
}And then in the event handler for node changed:
protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = ((TreeView)sender).SelectedNode;
if (node.GetType() == typeof(Child1TreeNode) )
{
// Do something related to Child1TreeNode types
}
else if (node.GetType() == typeof(Child2TreeNode) )
{
// Do something related to Child2TreeNode types
}
}Thanks. -Matt
------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
Hi there, What you are missing is that you also need to extend the
TreeView
control and override theCreateNode
method. This method is basically used on postback to recreate the nodes tree, and by default it returns theTreeNode
type and has no idea about your customChild2TreeNode
. -
Hi there, What you are missing is that you also need to extend the
TreeView
control and override theCreateNode
method. This method is basically used on postback to recreate the nodes tree, and by default it returns theTreeNode
type and has no idea about your customChild2TreeNode
.Web programming has all those little gotchas. I never seem to get over that. Anyhow, thanks for your help. Here's another question for you, though. How can I override the CreateNode method so that I can pass it an assortment of different objects that inherit from TreeNode and have them all retain their identity? Thanks again, -Matt
------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
-
Web programming has all those little gotchas. I never seem to get over that. Anyhow, thanks for your help. Here's another question for you, though. How can I override the CreateNode method so that I can pass it an assortment of different objects that inherit from TreeNode and have them all retain their identity? Thanks again, -Matt
------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall
Here you want to use multi different types of the tree node such as
Child1TreeNode
,Child2TreeNode
, so you need a way to determine which object should be created when the treenode is rebuilt on postback. And theViewState
comes to mind as an option when you can use it to persist the node type in order to recreate the tree node. However, when you create a tree node in the overridenCreateNode
method in the custom treeview, the ViewState of the tree node has not been loaded yet, so you will do that as soon as the ViewState is loaded. In the CreateNode, you simply return the base class of your two node types, sayChildTreeNode
, and you will replace this base node with the exact type after the node type is determined. Below is the sample code to demonstrate this thing:public class ExTreeView : TreeView { protected override TreeNode CreateNode() { return new ChildTreeNode(this); } } public class ChildTreeNode : TreeNode { public ChildTreeNode() { } public ChildTreeNode(string nodeType) { m\_NodeType = nodeType; } public ChildTreeNode(ExTreeView owner) { this.m\_Owner = owner; } private ExTreeView m\_Owner; internal ExTreeView Owner { get { return m\_Owner; } } private string m\_NodeType; internal string NodeType { get { return m\_NodeType; } set { m\_NodeType = value; } } protected override object SaveViewState() { object baseState = base.SaveViewState(); return new Pair(baseState, NodeType); } protected override void LoadViewState(object savedState) { Pair p = savedState as Pair; base.LoadViewState(p.First); NodeType = p.Second as string; //The node type is hard coded for the sake of simplicity. TreeNode newNode; if (NodeType == "Child1TreeNode") { newNode = new Child1TreeNode(); } else if (NodeType == "Child2TreeNode") { newNode = new Child2TreeNode(); } else { newNode = new TreeNode(); } newNode.Text = this.Text; newNode.Value = this.Value; ReplaceCurrentNode(this, newNode); }