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

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in Following code demonstrating the various ways of implementation.

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 -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.