469,946 Members | 1,771 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

TreeView in Web Custom Control

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

May 18 '06 #1
7 6053
What's the exception... the message and the stack trace..

Code looks prety ookie.

--
Happy Hacking,
Gaurav Vaish
http://www.mastergaurav.org
http://www.edujini.in
-------------------

May 19 '06 #2
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(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlTe xtWriter
output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:l ine 27
at System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at
System.Web.UI.HtmlControls.HtmlForm.RenderChildren (HtmlTextWriter
writer)
at
System.Web.UI.HtmlControls.HtmlContainerControl.Re nder(HtmlTextWriter
writer)
at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex tWriter
output)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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)

May 22 '06 #3
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" <jo**************@cab.se> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlTe xtWriter
output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:l ine 27
at System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at
System.Web.UI.HtmlControls.HtmlForm.RenderChildren (HtmlTextWriter
writer)
at
System.Web.UI.HtmlControls.HtmlContainerControl.Re nder(HtmlTextWriter
writer)
at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex tWriter
output)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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)

May 23 '06 #4
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(HtmlTe xtWriter writer) +31
System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter 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.Cal lback(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)" <ga*****************@nospam.gmail.com> wrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
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" <jo**************@cab.se> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlTe xtWriter
output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:l ine 27
at System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at
System.Web.UI.HtmlControls.HtmlForm.RenderChildren (HtmlTextWriter
writer)
at
System.Web.UI.HtmlControls.HtmlContainerControl.Re nder(HtmlTextWriter
writer)
at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex tWriter
output)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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)


Jun 7 '06 #5
I've given a solution to Jonas...
Just set the value of,... grrr... lost the property name, as empty
value.

BRB :)

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.org
http://www.edujini.in | http://webservices.edujini.in
-------------------
"Prakash V" <vv********@hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP03.phx.gbl...
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(HtmlTe xtWriter writer) +31
System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter 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.Cal lback(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)" <ga*****************@nospam.gmail.com> wrote
in message news:%2****************@TK2MSFTNGP04.phx.gbl...
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" <jo**************@cab.se> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlTe xtWriter
output) in C:\Work\WebSite1\WebControlLibrary1\MyControl.cs:l ine 27
at System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter
writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at
System.Web.UI.HtmlControls.HtmlForm.RenderChildren (HtmlTextWriter
writer)
at
System.Web.UI.HtmlControls.HtmlContainerControl.Re nder(HtmlTextWriter
writer)
at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex tWriter
output)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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(HtmlT extWriter
writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWrite r writer)
at System.Web.UI.Control.Render(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter
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)



Jun 11 '06 #6
I got it working by adding the following line:
treeview.SkipLinkText = "";
before treeview.RenderControl(writer).

Thanks alot Gaurav!

Jun 12 '06 #7
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(Int 32 index) +4107
System.Web.UI.WebControls.TreeNode.Render(HtmlText Writer writer, Int32
position, Boolean[] isLast, Boolean enabled) +2700
System.Web.UI.WebControls.TreeView.RenderContents( HtmlTextWriter writer)
+294
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer) +43
System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter 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.Cal lback(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" <jo**************@cab.se> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I got it working by adding the following line:
treeview.SkipLinkText = "";
before treeview.RenderControl(writer).

Thanks alot Gaurav!

Jun 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by SoKool | last post: by
reply views Thread by rh | last post: by
7 posts views Thread by vsiat | last post: by
18 posts views Thread by =?Utf-8?B?TGkgV2VuZw==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.