Compress/compact HTML during rendering

C

cpnet

I'm writing some custom controls, and I'd like to be able to remove all
unnecessary whitespace, etc. from my output. Ideally, I would just render
as normal, then remove this extra whitespace as the last step in the
rendering process. I'm sure I saw code to do this, but now I can't find it.
Is this something I have to do as I go, or can I somehow do this in a
single, last step of rendering?

Thanks,
cpnet
 
S

Steven Cheng[MSFT]

Hi Cpnet,

As for the problem you mentioned, here is my understanding:

if your custom control is a composite control which use the
"CreateChildControls" to generate the whole controls hierarchy, I think its
ok for use to override the "Render" method and do some modification on the
control's final output. We just need to create HtmlTextWriter through a
StringBuilder and call the
Control's base.Render to get the original Rendered html. Then, we can do
any modification on the stringbuilder as we like. For exmaple:

=====================================
protected override void Render(HtmlTextWriter output)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter(sb));
base.Render(writer);

//the output has been retrieved in the stringbuilder

//do the modification here

output.Write(sb.ToString());
}
======================================

HOpe helps. 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
 
C

cpnet

I thought I remembered there being a property on some class I could set that
would automatically take care of the whitespace removal, but your method
below will work well for me.

Thanks.
 
C

cpnet

As I looked at this a little more, I think maybe I need to change it a
little in case we end up getting an Html32TextWriter instead of a 'regular'
HtmlTextWriter from the output argument? I also found that if I called
"base.Render( tw)" then the output got rendered twice!

Here's what I came up with as an alternative:


protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
object[] createArgs = {new StringWriter(sb)};

//Make sure we're creating an instance of the
//same type of HtmlTextWriter as the original
HtmlTextWriter tw =
(HtmlTextWriter)Activator.CreateInstance(
writer.GetType(), createArgs);

try
{
//Calling base.Render here causes the
//output to be rendered twice?!?
//base.Render( tw);

RenderChildren( tw);
tw.Flush();

//Do stuff to remove whitespace

writer.Write( sb.ToString());
}
finally
{
tw.Close();
}
}
 
S

Steven Cheng[MSFT]

Hi Cpnet,

Thanks a lot for your followup and your additional suggestions. I really
appreciate. In addition, as for the
===========
"base.Render( tw)" then the output got rendered twice!
===========

problem you mentioned, I'm not sure whether there will be anything else
cause it. I've try both
base.Render(tw) or base.RenderChildren(tw) and didn't encounter this
problem. I simplely add some input controls in my test custom control in
CreateChildControl and they are only rendered once.
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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top