Re: Inheriting from the TreeNode class

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Alexander,

How are you defining the CustomTreeNodes in the CustomTreeView control?
I've just performed some test by statically declaring them in aspx template
or programmatically add through code, seems the CustomInt property can be
persited and read out well. Also, in the OnPopulate event, I can add child
nodes correctly.... Here is my test page and the code behind file:
(custom treeview and treenode class source remains the same as the one I
posted you last time):

=================aspx===========

<%@ Register Assembly="WebControlLib" Namespace="WebControlLib"
TagPrefix="cc1" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:CustomTreeView ID="CustomTreeView1" runat="server"
ExpandDepth="0"
OnSelectedNodeChanged="CustomTreeView1_SelectedNodeChanged"
OnTreeNodeExpanded="CustomTreeView1_TreeNodeExpanded"
OnTreeNodePopulate="CustomTreeView1_TreeNodePopulate"
>
<Nodes >

<cc1:CustomTreeNode Text="Node1" Value="Node1"
SelectAction="SelectExpand" >
<cc1:CustomTreeNode Text="Node11" Value="Node11"
SelectAction="SelectExpand" ></cc1:CustomTreeNode>
<cc1:CustomTreeNode Text="Node12"
Value="Node12"></cc1:CustomTreeNode>
<cc1:CustomTreeNode Text="Node13"
Value="Node13"></cc1:CustomTreeNode>
</cc1:CustomTreeNode>
<cc1:CustomTreeNode Text="Node2" Value="Node2">
<cc1:CustomTreeNode Text="Node21" Value="Node21"
PopulateOnDemand="True" CustomInt="0" ></cc1:CustomTreeNode>
<cc1:CustomTreeNode Text="Node22" Value="Node22"
PopulateOnDemand="True" CustomInt="1"></cc1:CustomTreeNode>
</cc1:CustomTreeNode>
</Nodes>
</cc1:CustomTreeView>

</div>
</form>
</body>

========code behind===========
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustTreeView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void CustomTreeView1_SelectedNodeChanged(object sender,
EventArgs e)
{
Response.Write("<br/>CustomTreeView1_SelectedNodeChanged ");
}
protected void CustomTreeView1_TreeNodeExpanded(object sender,
TreeNodeEventArgs e)
{
Response.Write("<br/>CustomTreeView1_TreeNodeExpanded ");
Response.Write("<br/>Node: " + e.Node.GetType());
}
protected void CustomTreeView1_TreeNodePopulate(object sender,
TreeNodeEventArgs e)
{
Response.Write("<br/>CustomTreeView1_TreeNodePopulate ");
WebControlLib.CustomTreeNode cn = e.Node as
WebControlLib.CustomTreeNode;
Response.Write("<br/>Node.CustomInt: " + cn.CustomInt );

for (int i = 0; i < 5; i++)
{
WebControlLib.CustomTreeNode ncn = new
WebControlLib.CustomTreeNode();
ncn.Text = "New Child Node " + i;
ncn.Value = cn.Text;

cn.ChildNodes.Add(ncn);
}
}
}
================================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Alexander Walker" <alex@xxxxxxxxxxxxxxx>
| References: <#7WFWGZGGHA.3100@xxxxxxxxxxxxxxxxxxxx>
<CSxlZblGGHA.3944@xxxxxxxxxxxxxxxxxxxxx>
| Subject: Re: Inheriting from the TreeNode class
| Date: Wed, 18 Jan 2006 22:27:31 -0000
| Lines: 140
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <uM#Je5HHGHA.140@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 84.12.20.49
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32578
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| 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
    ... When I tried to use the OnTreeNodePopulate event to access my custom node in 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 ... added will allow me to get a reference to a CustomTreeNode instance that has its ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)