Compressing ASP.NET 2.0 Viewstate

One thing I was looking forward with 2.0 release was improved viewstate optimization. The truth is it has improved, but for me there is still something desirable to be had.

Where I have run into the problems is a web application I am working on the must allow the user to add an infinate amount of web controls dynamically. The results below are pretty amazing. I ended up using SharpZipLib for the compressing algorithm.

Test Scenerio: One web page with that ended up with 116 web controls add to it.

Result of ViewState Size:
Before Compression: 43.3 KB
After Compression using SharpZipLib: 3.50 KB

Below is the code to get you started

Imports System.IO

Imports Zip = ICSharpCode.SharpZipLib.Zip.Compression


Public Class PageViewStateZip : Inherits System.Web.UI.Page


Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object

Dim vState As String = Me.Request.Form(“__VSTATE”)

Dim bytes As Byte() = System.Convert.FromBase64String(vState)

bytes = vioZip.Decompress(bytes)

Dim format As New LosFormatter


Return format.Deserialize(System.Convert.ToBase64String(bytes))


End Function


Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)

Dim format As New LosFormatter

Dim writer As New StringWriter

format.Serialize(writer, viewState)

Dim viewStateStr As String = writer.ToString()

Dim bytes As Byte() = System.Convert.FromBase64String(viewStateStr)

bytes = vioZip.Compress(bytes)

Dim vStateStr As String = System.Convert.ToBase64String(bytes)

RegisterHiddenField(“__VSTATE”, vStateStr)


End Sub


End Class



Public Class vioZip

Shared Function Compress(ByVal bytes() As Byte) As Byte()

Dim memory As New MemoryStream

Dim stream = New Zip.Streams.DeflaterOutputStream(memory, _

New Zip.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072)

stream.Write(bytes, 0, bytes.Length)


Return memory.ToArray()

End Function


Shared Function Decompress(ByVal bytes() As Byte) As Byte()

Dim stream = New Zip.Streams.InflaterInputStream(New MemoryStream(bytes))

Dim memory As New MemoryStream

Dim writeData(4096) As Byte

Dim size As Integer

While True

size = stream.Read(writeData, 0, writeData.Length)


If size > 0 Then memory.Write(writeData, 0, size) Else Exit While

End While


Return memory.ToArray()

End Function


End Class


A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at

42 thoughts on “Compressing ASP.NET 2.0 Viewstate

  1. stjoe says:

    Very cool, thanks for the info!

  2. [...] Here is the small tutorial on compressing ViewState in ASP.NET 2.0: Compressing ASP.NET 2.0 ViewState [...]

  3. Steve says:

    Very useful. thanks.

  4. Tony John says:

    Clenbuterol 40mcg T3/T4 Cytomel Special Offer Ketotifen clenbuterol, buy clenbuterol, clenbuterol weight loss, clenbuterol uk, order clenbuterol, cytomel t3, liquid clenbuterol, clenbuterol for sale, clenbuterol hydrochloride, clenbuterol side effects, clenbuterol side effects

  5. aubergine says:

    Thank you for sharing. Your blog is good.

  6. Thank you for sharing..You are very kind.

  7. Thank you very much for your sharing.

  8. Cool.Keep going. Thanks for your topic.

  9. Dr.Ted Cook says:

    Great..Good topic..many thanks.

  10. Thank you for your kind.

  11. Great. Thank you so much for sharing.

  12. Thank you very much for your sharing.

  13. Thank a lot for this information. It’s sound good.

  14. Cool topic. Thank you very much for your sharing.

  15. ???????????? says:

    Cool topic. Thank you very much for your kind.

  16. Thank you very much for sharing.

  17. That is great. Thank you for sharing.

  18. Thank you veru much for sharing your topic.

  19. cctv says:

    Great. Thank you very much for sharing your topic.

  20. Tony Fricger says:

    I just know that. Thank you for sharing.

  21. Thai Girls says:

    Wow. That is great.

  22. Event says:

    That is great. Thank you so much for sharing.

  23. CCTV says:

    Thank you for sharing.

  24. Great. Thank you for sharing.

  25. curtain says:

    Thank a lot for this information. It’s sound good.

  26. Great article – well worth the read.

  27. [...] ciao a tutti, stavo cercando di implementare questa soluzione per la riduzione del viewstate…t-20-viewstate Si fa uso della libreria SharpZipLib; ho scaricato l’assembly, l’ho messo nella directory bin del [...]

  28. pipip says:

    I’ve try this solution but it not complatible with ajax .

  29. André Azevedo says:

    Wow!!! Its really works!!! Thank you guys!!!

    Here, my c# translation:

    using System.IO;
    using Zip = ICSharpCode.SharpZipLib.Zip.Compression;

    public class BasePage: System.Web.UI.Page

    protected override object LoadPageStateFromPersistenceMedium()

    string _viewState = this.Request.Form["__VSTATE"];
    byte[] _bytes = System.Convert.FromBase64String(_viewState);

    _bytes = CompressToZip.Decompress(_bytes);

    LosFormatter _format = new LosFormatter();
    String _newViewState = Convert.ToBase64String(_bytes);

    return _format.Deserialize(_newViewState);

    protected override void SavePageStateToPersistenceMedium(object state)
    LosFormatter _format = new LosFormatter();
    StringWriter _writer = new StringWriter();

    _format.Serialize(_writer, state);

    String _viewStateString = _writer.ToString();

    byte[] _bytes = System.Convert.FromBase64String(_viewStateString);
    _bytes = CompressToZip.Compress(_bytes);

    String _newViewState = Convert.ToBase64String(_bytes);

    this.RegisterHiddenField(“__VSTATE”, _newViewState);



    public class CompressToZip

    public static byte[] Compress(byte[] _bytes)
    MemoryStream _memStream = new MemoryStream();
    Zip.Streams.DeflaterOutputStream _stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(_memStream,
    new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072);

    _stream.Write(_bytes, 0, _bytes.Length);

    return _memStream.ToArray();

    public static byte[] Decompress(byte[] _bytes)

    Zip.Streams.InflaterInputStream _stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(
    new MemoryStream(_bytes));

    MemoryStream _memStream = new MemoryStream();

    byte[] _writeData = new byte[4096];
    int _size = 0;

    while (true)
    _size = _stream.Read(_writeData, 0, _writeData.Length);

    if (_size > 0)
    _memStream.Write(_writeData, 0, _size);


    return _memStream.ToArray();


  30. That is great. Thanks…..

  31. Gangadhar says:

    The code was quite helpful…to improve the performance of the page ,which is quite overloaded….Thanks a lot for the code

  32. jar says:

    good job.

  33. To improve the performance of the page ,which is quite overloaded. Thank you so much for sharing.

  34. Wow! i agree! i’ve been searching for so long for a site where i could find everything that i want, and i’ve just found it!! really, i’ve visited your blog, and it’s amazing, i will keep visiting

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>