Re: Inheriting from the TreeNode class



Hi Steven

When I tried to use the OnTreeNodePopulate event to access my custom node in the
callback, the custom property did not have its value, I was forced to use the
FindNode method of the TreeView instance to get a reference to the
CustomTreeNode and it did have its value, only problem with using FindNode is
that the found node doesn't seem to allow child nodes to be added to it. Is
there something missing from my inherited TreeNode or TreeView class that once
added will allow me to get a reference to a CustomTreeNode instance that has its
CustomInt value using the following line of code in the OnTreeNodePopulate event

CustomTreeNode node = e.Node as ShopTreeNode;

Here is a brief example:

protected void CustomOnTreeNodePopulate(Object source, TreeNodeEventArgs e)
{
// get the CustomTreeNode instance
//
CustomTreeNode node1 = e.Node as ShopTreeNode;

// node1.CustomInt == 0 even though a value was
// given to it before the page rendered
//
int emptyCustomInt = node1.CustomInt;

// the following line is required to find the node
// and get a reference to it, but adding new nodes to
// the ChildNodes of this reference does not work,
// instead the old reference (node1) must be used
// for adding child nodes
//
CustomTreeNode node2 = customTreeView.FindNode(node1.ValuePath) as
CustomTreeNode;

// I can now access the CustomInt :)
//
int customInt = node2.CustomInt;

// I want to add a new node to the CustomTreeNode instance
//
CustomTreeNode node = new CustomTreeNode();
newNode.CustomInt = 5;

// the following line appears to have no effect :(
//
node2.ChildNodes.Add(newNode);
}


For the time being I can use the technique above but I think that my
CustomTreeView and CustomTreeNode are not very friendly because they dont behave
as they are expected to behave, somebody other than myself who is consuming the
CustomTreeView and CustomTreeNode classes would have a hard time figuring out
what was going on, they would either be figuring out why nodes added to the
CustomTreeNode instance dont appear or figuring out why the custom property has
lost its value, depending on how they obtain a reference to the CustomTreeNode
instance. Am I using the wrong technique to get a reference to the
CustomTreeNode instance in the OnTreeNodePopulate event? Any ideas on whats
missing from the CustomTreeView and CustomTreeNode class? Do I need to create a
new OnTreeNodePopulate event that has a CustomTreeNodeEventArgs argument?

Here is the source for the CustomTreeView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CustomTreeView : TreeView
{
protected override TreeNode CreateNode()
{
return new CustomTreeNode(this, false);
}
}

Here is the source for the CustomTreeNode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CustomTreeNode: TreeNode
{
private int customInt;

public CustomTreeNode()
: base()
{
}

public CustomTreeNode(
TreeView owner, bool isRoot)
: base(owner, isRoot)
{
}

protected override void LoadViewState(object state)
{
object[] arrState = state as object[];

this.customInt = (int)arrState[1];
base.LoadViewState(arrState[0]);
}

protected override object SaveViewState()
{
object[] arrState = new object[2];
arrState[0] = base.SaveViewState();
arrState[1] = this.customInt;;

return arrState;
}

public int CustomInt
{
get
{
return customInt;
}
set
{
customInt = value;
}
}
}


Thank you

Alex



.



Relevant Pages

  • Re: Inheriting from the TreeNode class
    ... | (custom treeview and treenode class source remains the same as the one I ... | protected void CustomTreeView1_SelectedNodeChanged(object sender, ... || FindNode method of the TreeView instance to get a reference to the ... || CustomTreeNode and it did have its value, ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Inheriting from the TreeNode class
    ... (custom treeview and treenode class source remains the same as the one I ... protected void CustomTreeView1_SelectedNodeChanged(object sender, ... | FindNode method of the TreeView instance to get a reference to the ... | CustomTreeNode and it did have its value, ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)

Loading