Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Compress/compact HTML during rendering

Reply
Thread Tools

Compress/compact HTML during rendering

 
 
cpnet
Guest
Posts: n/a
 
      09-08-2004
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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      09-09-2004
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


 
Reply With Quote
 
 
 
 
cpnet
Guest
Posts: n/a
 
      09-10-2004
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.


 
Reply With Quote
 
cpnet
Guest
Posts: n/a
 
      09-10-2004
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();
}
}


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      09-13-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IE6 SP1 rendering vs IE6 SP2 rendering Peter Mount HTML 4 01-31-2006 08:01 AM
Rendering the page during a delay Ambush ASP .Net 0 11-17-2005 12:56 AM
Rendering Custom Control during design time Joey Lee ASP .Net Building Controls 2 07-01-2005 03:35 AM
how can I write text from codebehind to html - without html rendering Alex Papadimoulis ASP .Net 3 05-06-2004 02:30 PM
Urgent!!!!!!!! read the values in text boxes without rendering during postback Ravindra ASP .Net 2 12-19-2003 07:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57