473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Render webcontrol to string

I need to get the HTML output from a webcontrol as a string instead of
it being rendered on the page.

I have the following code:
Dim SiteMapSource As New SiteMapDataSource()
Dim TreeView1 As New TreeView()
TreeView1.DataSource = SiteMapSource
TreeView1.ShowExpandCollapse = True
TreeView1.ExpandDepth = 1
TreeView1.DataBind()
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
Using tw As System.IO.StringWriter = New System.IO.StringWriter(sb)
Using hw As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(tw)
TreeView1.RenderControl(hw)
Response.Write(sb.ToString())
End Using
End Using

The line with TreeView1.RenderControl(hw) throws a
NullReferenceException with a almost unusable stacktrace:
[NullReferenceException: Object reference not set to an instance of an
object.]
System.Web.UI.Control.get_SpacerImageUrl() +59
System.Web.UI.WebControls.TreeView.RenderBeginTag( HtmlTextWriter
writer) +146
System.Web.UI.WebControls.WebControl.Render(HtmlTe xtWriter writer)
+17
System.Web.UI.Control.RenderControlInternal(HtmlTe xtWriter writer,
ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
SiteMapTest.Page_Load(Object sender, EventArgs e) in
D:\TreeView.aspx.vb:25
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1061

The weird part is that it works fine if I do
Page.Form.Controls.add(TreeView1) AND it works fine if I use a button
or even a Calendar control...

So, why does TreeView throw a NullReference when asked to render itself
?

Apr 20 '06 #1
0 1729

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jb_in_marietta | last post by:
All, I have written a very simple custom composite control that includes a control of type System.Web.UI.WebControls.Table. The control renders fine in run time, but for some reason, it does...
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
9
by: kw | last post by:
What is the proper way to get the element ID for a client script? For example, suppose in the WebControl: TextBox t=new TextBox; t.ID=this.ClientID+"X"; .... Then elsewhere we want to access...
1
by: Earl Teigrob | last post by:
After I have instanced a webcontrol (in code) and set its properties, I would like to get the html that it would render to the display. Can this be done. How??? Earl
3
by: spmm# | last post by:
Hi! Could someone please help me with the following; I have a WebControl that basically looks like this: public class LeftMenu : System.Web.UI.WebControls.WebControl { private string...
1
by: Alex Nitulescu | last post by:
Hi. What's the difference between the Render method (overridden when creating custom controls) and the RenderContents method.... (overridden when creating custom controls) ? From MSDN: ...
0
by: tommyw | last post by:
Hi everybody, I'm new to codecomments, but I really hope someone can help me. I am dynamically loading custom controls and user controls into my htmlform control. As I have a lot of "custom"...
1
by: WT | last post by:
Hello, I recently discovered that Page.Render is doing special things concerning the Menu navigation control. When I was using my own Render override, menus were not working, maybe some style...
6
by: Victor | last post by:
Hi. all I have a customize web control. I have three simple properties and customized render class. then I add this control into my datalist like <asp:DataList ID="datalist" runat="server"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.