About the author

Miron Abramson
Me
Software Engineer, Senior Developer at CapitalIQ, and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Optimize your page for UpdatePanel - part II

In the previews post about Optimize your page for UpdatePanel I talked about removing white spaces from the ASPX to optimize the Update Panel callbacks because it can't be compressed as normal response, and thats due the special  format that this response is (it needs to be parse be JS in the client side).

The Update Panel response contain the new html for the specific location that needs to be update and the complete new ViewState for the whole page. So, one more thing we can to optimize the UpdatePanel is to compress the ViewState before it been sent to the client. We don't need to do it in normal response because we use (or should use) a compression module that compress all the page response and that includes the ViewState. Compress the ViewState can save you some more KB from the async response. Another option is to save the ViewState in the server (file os session), and not send it at all. To compress the ViewState we needs to override the 'LoadPageStateFromPersistenceMedium' and the 'SavePageStateToPersistenceMedium' methods that load and save the view state.

Here is the code how to compress the ViewState for the UpdatePanel response only, just copy it to your base page:

 protected override object LoadPageStateFromPersistenceMedium()
{
    string viewState = Request.Form["__COMPRESSEDVS"];
    if (viewState != null)
    {
         byte[] data = Convert.FromBase64String(viewState);
         data = Utils.Decompress(data);
         LosFormatter lf = new LosFormatter();
         return lf.Deserialize(Convert.ToBase64String(data));
    }
    else
    {
         return base.LoadPageStateFromPersistenceMedium();
    }
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
    if (Utils.IsMsAjaxCallback(Request))
    {
         LosFormatter lf = new LosFormatter();
         using (StringWriter writer = new StringWriter())
         {
             lf.Serialize(writer, viewState);
             string viewStateString = writer.ToString();
             byte[] data = Convert.FromBase64String(viewStateString);
             data = Utils.Compress(data);
             ScriptManager.RegisterHiddenField(this, "__COMPRESSEDVS", Convert.ToBase64String(data));
         }
    }
    else
    {
         base.SavePageStateToPersistenceMedium(viewState);
    }
}

 (The class 'Utils' can be download here: Utils.cs (1.15 kb))

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Miron on Saturday, November 24, 2007 3:16 AM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

DotNetKicks.com

Saturday, November 24, 2007 3:23 AM

trackback

Trackback from DotNetKicks.com

Optimize your page for UpdatePanel - part II

John Burbidge

Thursday, September 04, 2008 5:37 AM

John Burbidge

I have a user control that contains an update panel and the script fails after a postback. Still useful, I haven't tested but I'm sure if I were to move the update panel to be outside the user control it would work.

Anyway thanks for the pointer.

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

Friday, November 21, 2008 12:29 AM