Issue with a custom TreeView derived from the TreeView control

Discussion in 'ASP .Net Web Controls' started by Ionutz, Oct 22, 2005.

  1. Ionutz

    Ionutz Guest

    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,
    Ionutz, Oct 22, 2005
    #1
    1. Advertising

  2. Ionutz

    Guest

    Ionutz wrote:
    > 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
    , Oct 23, 2005
    #2
    1. Advertising

  3. Ionutz

    Ionutz Guest

    "" wrote:
    >
    > 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.
    Ionutz, Oct 23, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    390
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  4. David
    Replies:
    3
    Views:
    390
    Grizlyk
    Jan 29, 2008
  5. MattC

    Custom Templated Databound Control or derived control?

    MattC, Nov 23, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    178
    MattC
    Nov 23, 2005
Loading...

Share This Page