RE: Inheriting from the TreeNode class
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Mon, 16 Jan 2006 04:39:22 GMT
Hi Alex,
Welcome to ASPNET newsgroup.
As for the creating custom TreeView with custom TreeNode class question,
based on my understanding we need to take care of the following things when
we need to extend the TreeNode or its properties:
1. For the Custom TreeNode's constructor, we'd better override the
TreeNode(TreeView owner, bool isRoot) one and call the base class's
constructor in our constructor . e.g:
protected override TreeNode CreateNode()
{
return new CustomTreeNode(this,false);
}
2. Also, for the properties in TreeNode, that won't be automatically saved
if we do not persist them when the page load/save states for the Control...
So for our CustomInt property, we have to override the TreeNode's
SaveViewState and LoadViewState (of IStateManager interface) to explictly
store the CustomInt value (in additional to base class's States infol...).
to make this clear, below is a simple modified version of the Custom
TreeView class and TreeNode class, you can have a look according to the
above things:
=======================
public class CustomTreeView : TreeView
{
protected override TreeNode CreateNode()
{
return new CustomTreeNode(this,false);
}
}
public class CustomTreeNode : TreeNode
{
private int customInt;
public int CustomInt
{
get
{
return customInt;
}
set
{
customInt = value;
}
}
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;
}
}
=============================
Hope helps. 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>
| Subject: Inheriting from the TreeNode class
| Date: Sun, 15 Jan 2006 05:07:28 -0000
| Lines: 63
| 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
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#7WFWGZGGHA.3100@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 84-12-165-60.dyn.gotadsl.co.uk 84.12.165.60
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32475
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hello
|
| I have a written a class that inherits from the TreeNode class
|
| I want to use the inherited TreeNode class to attach custom properties to
| the nodes in a TreeView web control
|
| I am trying to use the TreeNodePopulate event to read the custom property
| when the user expands a node
|
| The problem is that the custom node is not recognised when the event
fires
| after the page has been rendered
|
| How can I access the custom node during postbacks, I get the feeling that
| the custom node isn't being created, even though I've overridden the
| CreateNode method, is there something else I should do? Do I need to
| override other methods that persist the custom properties of the custom
| treenode? Am I doing this the wrong way? Should I take an entirely
different
| approach?
|
| Here is some of the code
|
| public class CustomTreeView : TreeView
| {
| protected override TreeNode CreateNode()
| {
| return new CustomTreeNode();
| }
| }
|
| public class CustomTreeNode : TreeNode
| {
| private int customInt;
|
| public int CustomInt
| {
| get
| {
| return customInt;
| }
| set
| {
| customInt= value;
| }
| }
|
| }
|
|
| protected void customTreeView_TreeNodePopulate(object sender,
| TreeNodeEventArgs e)
| {
| CustomTreeNode node = e.Node as CustomTreeNode;
| // node == null
| // i need to access node.CustomInt
| ...
| }
|
| Thanks
|
| Alex
|
|
|
.
- Follow-Ups:
- Re: Inheriting from the TreeNode class
- From: Alexander Walker
- Re: Inheriting from the TreeNode class
- References:
- Inheriting from the TreeNode class
- From: Alexander Walker
- Inheriting from the TreeNode class
- Prev by Date: Re: 3-Tier Binding Problem ?????????
- Next by Date: RE: CreateChildControls() always returns default property values
- Previous by thread: Inheriting from the TreeNode class
- Next by thread: Re: Inheriting from the TreeNode class
- Index(es):
Relevant Pages
|