TreeView in Web Custom Control

Discussion in 'ASP .Net Building Controls' started by Jonas, May 18, 2006.

  1. Jonas

    Jonas Guest

    Hi.
    I'm trying to develop a web custom control that uses a programmatically
    created treeview.
    My problem is that I get an exception when I try to render the control.
    With properties do I have to assign to get this working.
    Here is some sample code:

    namespace WebControlLibrary1
    {
    [ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
    public class MyControl : WebControl
    {
    protected override void RenderContents(HtmlTextWriter output)
    {
    XmlDataSource source = new XmlDataSource();
    source.Data = GetXmlElement().OuterXml;

    TreeView tv = new TreeView();
    tv.ID = ClientID + "_TreeView";
    tv.ImageSet = TreeViewImageSet.XPFileExplorer;
    tv.RenderControl(output);
    }

    private XmlElement GetXmlElement()
    {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<Xml><EmptyXml /></Xml>");
    return doc.DocumentElement;
    }
    }
    }


    Best regards
    Jonas Samuelsson
     
    Jonas, May 18, 2006
    #1
    1. Advertising

  2. Jonas

    Jonas Guest

    Hi,
    here is the exception info:

    System.NullReferenceException was unhandled by user code
    Message="Object reference not set to an instance of an object."
    Source="System.Web"
    StackTrace:
    at System.Web.UI.Control.get_SpacerImageUrl()
    at
    System.Web.UI.WebControls.TreeView.RenderBeginTag(HtmlTextWriter
    writer)
    at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter
    writer)
    at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    writer, ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    at WebControlLibrary1.MyControl.RenderContents(HtmlTextWriter
    output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:line 27
    at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter
    writer)
    at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    writer, ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter
    writer, ICollection children)
    at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
    at
    System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter
    writer)
    at
    System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter
    writer)
    at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter
    output)
    at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    writer, ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    at
    System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter
    writer)
    at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter
    writer, ICollection children)
    at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
    at System.Web.UI.Control.Render(HtmlTextWriter writer)
    at System.Web.UI.Page.Render(HtmlTextWriter writer)
    at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    writer, ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    ControlAdapter adapter)
    at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    at System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     
    Jonas, May 22, 2006
    #2
    1. Advertising

  3. Jonas

    Prakash V Guest

    Hi ,
    i too got the same problem, i try to render a treecontrol and pass
    the html to some other mehthod, it give object set to null exception, i
    tried many things but cannot do it, i tried to replace get_spacerImageurl()
    but no use. Did anyone have solution for this problem

    part of my code is
    private string RenderTree()

    {


    TreeView Tview = new TreeView();

    TreeNode tnode = new TreeNode("Node1", "1");

    Tview.Nodes.Add(tnode);

    Tview.ID = "Tree1";


    StringBuilder sb = new StringBuilder();

    StringWriter sw = new StringWriter(sb);

    HtmlTextWriter htmlTW = new HtmlTextWriter(sw);

    Tview.RenderControl(htmlTW);

    return sb.ToString();

    }

    and stack trace is [NullReferenceException: Object reference not set to an
    instance of an object.]
    System.Web.UI.Control.get_SpacerImageUrl() +50
    System.Web.UI.WebControls.TreeView.RenderBeginTag(HtmlTextWriter writer)
    +202
    System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +31
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,
    ControlAdapter adapter) +74
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter
    adapter) +291
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +49
    TreeviewRender.RenderTree() in
    d:\TestProject\TestProject\TreeviewRender.aspx.cs:30
    TreeviewRender.Page_Load(Object sender, EventArgs e) in
    d:\TestProject\TestProject\TreeviewRender.aspx.cs:17
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
    Object t, EventArgs e) +31
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
    EventArgs e) +68
    System.Web.UI.Control.OnLoad(EventArgs e) +88
    System.Web.UI.Control.LoadRecursive() +74
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3030







    "Gaurav Vaish (EduJini.IN)" <> wrote in
    message news:%...
    > Cryptic only with the exception that SpacerImageUrl is missing.
    > Provided any value to it?
    > Or may be something coming out of MyControl.cs
    >
    > Mail me the relevant code (with aspx) at
    > gaurav~DOT~vaish[at]gmail~DOT~com... not sure, but probably may be of some
    > help.
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish
    > http://www.mastergaurav.org
    > http://www.edujini.in
    > -------------------
    >
    >
    > "Jonas" <> wrote in message
    > news:...
    >> Hi,
    >> here is the exception info:
    >>
    >> System.NullReferenceException was unhandled by user code
    >> Message="Object reference not set to an instance of an object."
    >> Source="System.Web"
    >> StackTrace:
    >> at System.Web.UI.Control.get_SpacerImageUrl()
    >> at
    >> System.Web.UI.WebControls.TreeView.RenderBeginTag(HtmlTextWriter
    >> writer)
    >> at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter
    >> writer)
    >> at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    >> writer, ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    >> ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    >> at WebControlLibrary1.MyControl.RenderContents(HtmlTextWriter
    >> output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:line 27
    >> at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter
    >> writer)
    >> at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    >> writer, ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    >> ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    >> at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter
    >> writer, ICollection children)
    >> at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
    >> at
    >> System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter
    >> writer)
    >> at
    >> System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter
    >> writer)
    >> at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter
    >> output)
    >> at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    >> writer, ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    >> ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    >> at
    >> System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter
    >> writer)
    >> at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter
    >> writer, ICollection children)
    >> at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
    >> at System.Web.UI.Control.Render(HtmlTextWriter writer)
    >> at System.Web.UI.Page.Render(HtmlTextWriter writer)
    >> at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter
    >> writer, ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    >> ControlAdapter adapter)
    >> at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
    >> at System.Web.UI.Page.ProcessRequestMain(Boolean
    >> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    >>

    >
    >
     
    Prakash V, Jun 7, 2006
    #3
  4. Jonas

    Jonas Guest

    I got it working by adding the following line:
    treeview.SkipLinkText = "";
    before treeview.RenderControl(writer).

    Thanks alot Gaurav!
     
    Jonas, Jun 12, 2006
    #4
  5. Jonas

    Prakash V Guest

    Hi,
    after adding treeview.SkipLinkText = ""; also it's not working, it now
    expects GetImageUrl. i am giving my code and stack trace below.

    public partial class TreeviewRender : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    if (IsPostBack)

    RenderTree();

    }

    private string RenderTree()

    {

    TreeView Tview = new TreeView();

    TreeNode tnode = new TreeNode("Node1", "1");

    Tview.Nodes.Add(tnode);

    Tview.ID = "Tree1";

    Tview.SkipLinkText = "";

    StringBuilder sb = new StringBuilder();

    StringWriter sw = new StringWriter(sb);

    HtmlTextWriter htmlTW = new HtmlTextWriter(sw);

    Tview.RenderControl(htmlTW);

    return sb.ToString();

    }

    }


    Source Error:


    Line 30: StringWriter sw = new StringWriter(sb);
    Line 31: HtmlTextWriter htmlTW = new HtmlTextWriter(sw);
    Line 32: Tview.RenderControl(htmlTW);
    Line 33: return sb.ToString();
    Line 34:


    Source File: d:\TestProject\TestProject\TreeviewRender.aspx.cs Line: 32

    Stack Trace:


    [NullReferenceException: Object reference not set to an instance of an
    object.]
    System.Web.UI.WebControls.TreeView.GetImageUrl(Int32 index) +4107
    System.Web.UI.WebControls.TreeNode.Render(HtmlTextWriter writer, Int32
    position, Boolean[] isLast, Boolean enabled) +2700
    System.Web.UI.WebControls.TreeView.RenderContents(HtmlTextWriter writer)
    +294
    System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +43
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,
    ControlAdapter adapter) +74
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter
    adapter) +291
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +49
    TreeviewRender.RenderTree() in
    d:\TestProject\TestProject\TreeviewRender.aspx.cs:32
    TreeviewRender.Page_Load(Object sender, EventArgs e) in
    d:\TestProject\TestProject\TreeviewRender.aspx.cs:18
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
    Object t, EventArgs e) +31
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
    EventArgs e) +68
    System.Web.UI.Control.OnLoad(EventArgs e) +88
    System.Web.UI.Control.LoadRecursive() +74
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3034



    Thanks in Advance,
    Prakash.V
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
    Version:2.0.50727.42

    "Jonas" <> wrote in message
    news:...
    >I got it working by adding the following line:
    > treeview.SkipLinkText = "";
    > before treeview.RenderControl(writer).
    >
    > Thanks alot Gaurav!
    >
     
    Prakash V, Jun 12, 2006
    #5
    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. Jonas

    TreeView in Web Custom Control

    Jonas, May 18, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    6,238
    itaisc
    Oct 11, 2007
  2. Matt Sokol
    Replies:
    2
    Views:
    388
    Theo Tillotson
    Aug 7, 2003
  3. Ionutz
    Replies:
    2
    Views:
    534
    Ionutz
    Oct 23, 2005
  4. Jonas

    TreeView in Web Custom Control

    Jonas, May 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    231
    Prakash V
    Jun 12, 2006
  5. Replies:
    0
    Views:
    212
Loading...

Share This Page