Issue with a custom TreeView derived from the TreeView control

I

Ionutz

Hello!
I derived a control from the standard TreeView control of ASP.NET 2.0, which
uses custom tree nodes derived from the TreeNode class.

The problem is that when I set the property PopulateOnDemand = true on a
node of my tree view and try to expand the node, I receive a
System.NullReferenceException with the folowing stack trace:

at System.Web.UI.WebControls.TreeNode.RenderChildNodes(HtmlTextWriter
writer, Int32 depth, Boolean[] isLast, Boolean enabled)
at System.Web.UI.WebControls.TreeView.RaiseCallbackEvent(String
eventArgument)
at
System.Web.UI.WebControls.TreeView.System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


When I don't use the populate on demand feature everything works fine.

Here is a snippet of the code that I wrote:

public class MyTreeView : TreeView
{
...
protected override TreeNode CreateNode()
{
return new MyTreeNode();
}
...
}

public class MyTreeNode : TreeNode
{
public MyTreeNode(string text) : base(text)
{
}
}

Also I handled the event TreeNodePopulate to get my nodes data.

Does anyone encountered this problem and is there a workaround for this ?
I mention that I use VS 2005 beta 2.

Thank you,
 
M

markdemich

Ionutz said:
Hello!
I derived a control from the standard TreeView control of ASP.NET 2.0, which
uses custom tree nodes derived from the TreeNode class.

The problem is that when I set the property PopulateOnDemand = true on a
node of my tree view and try to expand the node, I receive a
System.NullReferenceException with the folowing stack trace:

at System.Web.UI.WebControls.TreeNode.RenderChildNodes(HtmlTextWriter
writer, Int32 depth, Boolean[] isLast, Boolean enabled)
at System.Web.UI.WebControls.TreeView.RaiseCallbackEvent(String
eventArgument)
at
System.Web.UI.WebControls.TreeView.System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


When I don't use the populate on demand feature everything works fine.

Here is a snippet of the code that I wrote:

public class MyTreeView : TreeView
{
...
protected override TreeNode CreateNode()
{
return new MyTreeNode();
}
...
}

public class MyTreeNode : TreeNode
{
public MyTreeNode(string text) : base(text)
{
}
}

Also I handled the event TreeNodePopulate to get my nodes data.

Does anyone encountered this problem and is there a workaround for this ?
I mention that I use VS 2005 beta 2.

Thank you,

Sorry, I don't know the answer to your question, maybe if you use
Reflector and check out the framework code it might give you an idea of
what's going on. On another note, I have a question of my own that
maybe you can answer. I was attempting to override the TreeView and
TreeNode controls so I could alter the nodes render method. I was
trying to add columns. Unfortunately, I discovered that that most of
the methods I needed to override were private. Can I as what you're
doing in your custom controls?

Thanks,
Mark
 
I

Ionutz

Sorry, I don't know the answer to your question, maybe if you use
Reflector and check out the framework code it might give you an idea of
what's going on. On another note, I have a question of my own that
maybe you can answer. I was attempting to override the TreeView and
TreeNode controls so I could alter the nodes render method. I was
trying to add columns. Unfortunately, I discovered that that most of
the methods I needed to override were private. Can I as what you're
doing in your custom controls?

Thanks,
Mark

I found where the problem comes from.
I used the Reflector.NET and I observed the following code for the
CreateNode method:

protected internal virtual TreeNode CreateNode()
{
return new TreeNode(this, false);
}

The problem is that this constructor of TreeNode is declared as internal and
when I create an instance of MyTreeNode I can't specify that MyTreeView is
the owner of the node.
The exception is raised because RenderChildNodes method uses _owner.Style to
render the stuff,
but in my case the _owner is null.

I hope the guys from Microsoft will resolve this bug in the release version.

And another thing.... I saw that the CreateNode method is declared as
"protected internal virtual"...If this method is internal how am I able to
override it ?!

And to respond to your question, Mark ... I subclassed the TreeView because
I wanted to add drag and drop functionality to the nodes.
 

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

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top