Extracting HTML from a WebControl

R

Russ

Hi,

I have created a Table webcontrol dynamically using code. Before rendering
it, I need to get at the HTML and do a couple of things manually. I've
tried a couple of methods to extract the HTML but things aren't working as
expected.

Dim W As New System.IO.StringWriter
Dim TS As New HtmlTextWriter(W)
Dim S as String
Table.RenderControl(TS)
W.Write(S)

When I run this, S = nothing, but the table renders fine on the page.

Any ideas?

Thanks,

Russ
 
T

Toby Mathews

Russ,

Why do you need to extract the HTML? There may be another way to achieve
what you want without doing this.

Toby Mathews
 
S

Steven Cheng[MSFT]

Hi Russ,

As for getting the output html of a webcontrol, the code you used is
correct , just one thing missed. We need to use a StringBuilder to
construct the StringWriter and after rendering the control into the
HtmlTextWriter, we cna get the control's output in the StringBuilder. For
example:

=========================================
Table tb = new Table();
tb.ID = "tbl";
tb.Rows.Add(new TableRow());
tb.Rows.Add(new TableRow());
tb.Rows[0].Cells.Add(new TableCell());
tb.Rows[1].Cells.Add(new TableCell());

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

tb.RenderControl(htw);

Response.Write("<br>" + HttpUtility.HtmlEncode(sb.ToString()));

htw.Close();
sw.Close();
==================================

In addition, it is ok to retrieve the outputhtml of a control, however,
haven't means to easiliy replace the ouput which will be rendered in the
page. If you want to make some customizing on the control's output, it's
better that we override the control. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top