Skip to main content

Compressing viewstate in

Viewstate as we know basically is a serialized data which stores in pages in the form of hidden fields. As we know that viewstate stores in the page so it makes page size larger (increase page data size), so if your viewstate is not much there is no problem but if viewstate is enough larger then it would be the cause of slower page load and decrease
website performance, then there is the need of viewstate compression to minimizing viewstate size as much as possible.

How to compress and uncompress viewstate data is the question arising in our mind first and when to compress viewstate which would be able to make page performance faster than before, So here is the code for minimizing viewstate size as much possible, but beware, we are minimizing viewstate size by the code only for increasing page performance and decreasing page data size not for decreasing performance so please don't create overhead (don't compress, uncompress viewstate) if viewstate is not as much larger as it would be the cause the slower performance.

Paste the given code on the page for which you want to minimizing viewstate size.
use the following namespace for code working properly.

using System.IO;
using System.Text;
using System.IO.Compression;

private ObjectStateFormatter osFormatter = new ObjectStateFormatter();
private static byte[] Compress(byte[] data)
    MemoryStream outputCompress = new MemoryStream();
    GZipStream compressStream = new GZipStream(outputCompress, CompressionMode.Compress, true);
    compressStream.Write(data, 0, data.Length);
    return outputCompress.ToArray();
private static byte[] Uncompress(byte[] data)
    MemoryStream compressedData = new MemoryStream();
    compressedData.Write(data, 0, data.Length);
    compressedData.Position = 0;
    GZipStream compressStream = new GZipStream(compressedData, CompressionMode.Decompress, true);
    MemoryStream uncompressedData = new MemoryStream();
    byte[] buffer = new byte[data.Length];
    int read = compressStream.Read(buffer, 0, buffer.Length);

    while (read > 0)
        uncompressedData.Write(buffer, 0, read);
        read = compressStream.Read(buffer, 0, buffer.Length);
    return uncompressedData.ToArray();
protected override void SavePageStateToPersistenceMedium(object viewState)
    MemoryStream ms = new MemoryStream();
    osFormatter.Serialize(ms, viewState);
    byte[] viewStateArray = ms.ToArray();
    ClientScript.RegisterHiddenField("__COMPRESSED_VIEWSTATE", Convert.ToBase64String(Compress(viewStateArray)));
protected override object LoadPageStateFromPersistenceMedium()
    var compressedViewState = Request.Form["__COMPRESSED_VIEWSTATE"];
    var bytes = Uncompress(Convert.FromBase64String(compressedViewState));
    return osFormatter.Deserialize(Convert.ToBase64String(bytes));

Popular posts from this blog

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Merging multiple PDFs using iTextSharp in c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

Uploading large file in chunks in Mvc c# from Javascript ajax

Often we have a requirement to upload files in, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)