Compress/compact HTML during rendering

Discussion in 'ASP .Net Building Controls' started by cpnet, Sep 9, 2004.

  1. cpnet

    cpnet Guest

    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
     
    cpnet, Sep 9, 2004
    #1
    1. Advertising

  2. 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
     
    Steven Cheng[MSFT], Sep 9, 2004
    #2
    1. Advertising

  3. cpnet

    cpnet Guest

    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.
     
    cpnet, Sep 10, 2004
    #3
  4. cpnet

    cpnet Guest

    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();
    }
    }
     
    cpnet, Sep 10, 2004
    #4
  5. 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
     
    Steven Cheng[MSFT], Sep 13, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravindra
    Replies:
    2
    Views:
    453
    Natty Gur
    Dec 19, 2003
  2. Untitled

    Automatically Compact HTML on render

    Untitled, Aug 16, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    777
    Karl Seguin
    Aug 16, 2005
  3. Ambush

    Rendering the page during a delay

    Ambush, Nov 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    387
    Ambush
    Nov 17, 2005
  4. Peter Mount
    Replies:
    4
    Views:
    1,003
    Peter Mount
    Jan 31, 2006
  5. Joey Lee

    Rendering Custom Control during design time

    Joey Lee, Jun 30, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    169
    Joey Lee
    Jul 1, 2005
Loading...

Share This Page