Custom TreeNode typing lost after postback


J

John Bankhead

[.NET 2.0]
I have inherited from TreeNode to create my own FaultTreeNode, which
contains three additional properties and some modifications to RenderPreText
and RenderPostText. I have no problem adding instances of FaultTreeNode to a
standard TreeView. Rendering of the nodes occurs as expected. I can step
thru the code and see that the nodes are of type FaultTreeNode.

The problem occurs during the postback. The treeview is recreated by the
system, but the treenodes are no longer of type FaultTreeNode, but just
TreeNode. Nor am I able to cast them to FaultTreeNodes. [Unable to cast
object of type 'System.Web.UI.WebControls.TreeNode' to type
'Volvo.VITNA.VolvoLink.Insight.FaultTreeNode'] As a result, I am no longer
able to access the information stored in those additional properties.

Below is a simplified representation of my code. is there something wrong
with the approach?

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FaultTreeNode _root = new FaultTreeNode("Fault Code Filter", -1,
"");
_root.FilterType = NodeType.NONE;
_root.ChildNodes.Add(AddChildNodes());
treeCtrl.Nodes.Add(_root);
}
ListCheckedNodes(tree.Nodes);
}

void ListCheckedNodes(TreeNodeCollection nodes)
{
int count = nodes.Count;
for (int i = 0; i < count; i++)
{
try
{
FaultTreeNode ftn = (FaultTreeNode)(nodes);
if (ftn.FilterType == NodeType.NONE)
ListCheckedNodes(nodes.ChildNodes);
{
if (ftn.Checked) submitted.Items.Add(ftn.Text);
}
}
catch (Exception ex)
{
string x = ex.Message;
x = x;
}
}
}
 
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top