Compressing ASP.NET 2.0 Viewstate

One thing I was looking forward with asp.net 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 asp.net 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)

stream.Close()

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

stream.Close()

Return memory.ToArray()

End Function

 

End Class

Written by Tim on March 2nd, 2006 with 42 comments.
Read more articles on asp.net.

Related articles

42 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com stjoe
#1. March 3rd, 2006, at 12:39 AM.

Very cool, thanks for the info!

Trackback Mention from Firoz.name
#2. May 18th, 2006, at 3:44 PM.

Firoz Ansari » Blog Archive » Compressing ASP.NET 2.0 ViewState: Here is the small tutorial on compressing ViewState in ASP.NET 2.0: Compressing ASP.NET 2.0 ViewState

Get your own gravatar by visiting gravatar.com Steve
#3. May 24th, 2006, at 6:30 PM.

Very useful. thanks.

Get your own gravatar by visiting gravatar.com Tony John
#4. December 8th, 2007, at 11:04 AM.

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

http://www.clenbuterol.co.uk

Get your own gravatar by visiting gravatar.com aubergine
#5. December 11th, 2007, at 4:30 AM.

Thank you for sharing. Your blog is good.

Get your own gravatar by visiting gravatar.com Suwana Teeraporn
#6. December 26th, 2007, at 4:18 AM.

Thank you for sharing..You are very kind.

Get your own gravatar by visiting gravatar.com porntip sriyana
#7. January 9th, 2008, at 10:57 AM.

Thank you very much for your sharing.

Get your own gravatar by visiting gravatar.com Dr. Michael Marks
#8. January 12th, 2008, at 3:37 AM.

Cool.Keep going. Thanks for your topic.

Get your own gravatar by visiting gravatar.com Dr.Ted Cook
#9. January 12th, 2008, at 9:15 AM.

Great..Good topic..many thanks.

Get your own gravatar by visiting gravatar.com Timothy Annis
#10. January 14th, 2008, at 12:19 PM.

Thank you for your kind.

Get your own gravatar by visiting gravatar.com Spencer Hoffman
#11. January 15th, 2008, at 8:54 AM.

Great. Thank you so much for sharing.

Get your own gravatar by visiting gravatar.com Ernesto Cardozo
#12. January 19th, 2008, at 5:52 AM.

Thank you very much for your sharing.

Get your own gravatar by visiting gravatar.com Sombat Tiyayon
#13. January 24th, 2008, at 1:19 PM.

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

Get your own gravatar by visiting gravatar.com Jantira Songbun
#14. February 5th, 2008, at 5:22 AM.

Cool topic. Thank you very much for your sharing.

Get your own gravatar by visiting gravatar.com ????????????
#15. February 9th, 2008, at 6:05 PM.

Cool topic. Thank you very much for your kind.

Get your own gravatar by visiting gravatar.com Evan Richards
#16. February 12th, 2008, at 4:43 PM.

Thank you very much for sharing.

Get your own gravatar by visiting gravatar.com Bangkok real estate agent
#17. February 15th, 2008, at 6:48 AM.

That is great. Thank you for sharing.

Get your own gravatar by visiting gravatar.com Wichada Tammaruk
#18. February 17th, 2008, at 8:04 AM.

Thank you veru much for sharing your topic.

Get your own gravatar by visiting gravatar.com cctv
#19. February 18th, 2008, at 4:07 PM.

Great. Thank you very much for sharing your topic.

Get your own gravatar by visiting gravatar.com Tony Fricger
#20. February 22nd, 2008, at 4:48 AM.

I just know that. Thank you for sharing.

Get your own gravatar by visiting gravatar.com Thai Girls
#21. March 25th, 2008, at 2:00 AM.

Wow. That is great.

Get your own gravatar by visiting gravatar.com Event
#22. April 2nd, 2008, at 6:51 AM.

That is great. Thank you so much for sharing.

Get your own gravatar by visiting gravatar.com Walia Chiropractic Back Pain Relief Center
#23. April 17th, 2008, at 7:58 AM.

Great. Thank you for sharing.

Get your own gravatar by visiting gravatar.com CCTV
#24. April 22nd, 2008, at 3:35 AM.

Thank you for sharing.

Get your own gravatar by visiting gravatar.com Video Conference
#25. April 28th, 2008, at 8:48 AM.

Thank you.

Get your own gravatar by visiting gravatar.com Watertown neck pain
#26. May 21st, 2008, at 6:02 AM.

Great. Thank you for sharing.

Get your own gravatar by visiting gravatar.com curtain
#27. June 20th, 2008, at 7:24 AM.

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

Get your own gravatar by visiting gravatar.com maternity clothes
#28. July 5th, 2008, at 4:18 AM.

OK. Thanks.

Get your own gravatar by visiting gravatar.com ????????????
#29. July 8th, 2008, at 10:00 AM.

thank

Get your own gravatar by visiting gravatar.com maternity clothes
#30. August 28th, 2008, at 2:00 AM.

Great article – well worth the read.

Get your own gravatar by visiting gravatar.com Cooling Tower
#31. September 15th, 2008, at 9:17 AM.

Thanks.

Get your own gravatar by visiting gravatar.com Thailand port
#32. September 15th, 2008, at 9:18 AM.

Thank you so much.

Trackback Mention from Hilpers.it
#33. January 21st, 2009, at 3:47 PM.

IcSharp Code e compressione viewstate | hilpers: ciao a tutti, stavo cercando di implementare questa soluzione per la riduzione del viewstate http://www.techtoolblog.com/archives...t-20-viewstate ...

Get your own gravatar by visiting gravatar.com pipip
#34. April 7th, 2009, at 4:12 PM.

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

Get your own gravatar by visiting gravatar.com André Azevedo
#35. April 7th, 2009, at 6:28 PM.

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);
_stream.Close();

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);
else
break;
}

_stream.Close();

return _memStream.ToArray();
}

}

Get your own gravatar by visiting gravatar.com Escorts in London
#36. April 24th, 2009, at 8:52 AM.

Cool. Thanks.

Get your own gravatar by visiting gravatar.com hotel bangkok
#37. June 2nd, 2009, at 10:54 AM.

That is great. Thanks…..

Get your own gravatar by visiting gravatar.com Gangadhar
#38. June 3rd, 2009, at 2:12 PM.

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

Get your own gravatar by visiting gravatar.com jar
#39. July 5th, 2009, at 7:00 AM.

good job.

Get your own gravatar by visiting gravatar.com 24 hour escorts london
#40. November 6th, 2009, at 5:00 AM.

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

Get your own gravatar by visiting gravatar.com Shared Web Hosting
#41. December 21st, 2009, at 3:08 PM.

good job

Get your own gravatar by visiting gravatar.com sharmawebservice
#42. January 19th, 2010, at 2:38 AM.

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 your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .