Jwalin Khatri

Optimize your page for UpdatePanel

Posted by: jwalin on: March 26, 2008

Than’s to Miron 

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);
    }
}
=================
HERE IS THE Utils.cs CLASS
=================
using System;
using System.Web;
using System.IO;
using System.IO.Compression;
public class Utils
{
    public static bool IsMsAjaxCallback(HttpRequest request)
    {
        return (request != null && request.Headers["X-MicrosoftAjax"] != null);
    }
    public static byte[] Compress(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(data, 0, data.Length);
                zip.Dispose();
                return ms.ToArray();
            }
        }
    }
    public static byte[] Decompress(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            int dataLength = BitConverter.ToInt32(data, 0);
            ms.Write(data, 0, data.Length);
            byte[] buffer = new byte[dataLength];
            ms.Position = 0;
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
            {
                zip.Read(buffer, 0, buffer.Length);
            }
            return buffer;
        }
    }
}
 

Leave a Reply

Blog Stats

  • 16,379 hits

  • Kingsley Tagbo: Thanks for reading and sharing my article. Have a great like ... Kingsley
  • Bob: Doesn't work with IIS7 (default config on Windows 7 and 2008) because no ADSI anymore.
  • jwalin: Follow the following steps 1]. open you Page in NOTEPAD. 2]. find the tag 3]. Replace the "body" like body oncontextmenu=”return false;" 4]. Sav