ASP. NET application ViewState Technology

Source: Internet
Author: User

The characteristics of MS's application of ViewState Technology in ASP. NET. ASP. NET application ViewState technology, in essence, ViewState technology uses a Hidden form field named "_ VIEWSTATE by default to store and transmit data. The data is a Base64 encoded string value after serialization, and in the method Page. savePageStateToPersistenceMedium is saved before output and is stored by Page. loadPageStateFromPersistenceMedium load ). Although we can easily disable round-trip transmission of the data at three levels:

In Machine. config, set <pages enableViewStateMac = 'false'/>
Set <pages enableViewStateMac = 'false'/>
Set <enableViewStateMac = 'false' %> on a single Page or set Page. EnableViewStateMac = false by code;

However, if we can completely disable ViewState to solve the data transmission burden without side effects, so MS architects will not be so stupid as to be so cute, what can they do with it ?), Because we often cannot solve this transmission burden problem by simply disabling it, we can only set another path to make it as small as possible in the round-trip network, so compression is our first choice. You only need to reload the SavePageStateToPersistenceMedium method and LoadPageStateFromPersistenceMedium method of the Page class, and compress and decompress the data in the reload method. The class GZipInputStream and GZipOutputStream provided by the open-source project SharpZipLib enter our field of view. For convenience, you may write a class CompressionHelper. The Code is as follows:

 
 
  1. Using System. IO;
  2. Using ICSharpCode. SharpZipLib. GZip;
  3.  
  4. Namespace Ycweb. Components
  5. {
  6. /**////<Summary> 
  7. /// Summary description for CompressionHelper.
  8. ///</Summary> 
  9. Public class CompressionHelper
  10. {
  11. Public CompressionHelper ()
  12. {
  13. //
  14. // TODO: Add constructor logic here
  15. //
  16. }
  17.  
  18. /**////<Summary> 
  19. /// Compress data
  20. ///</Summary> 
  21. ///<Param Name="Data">Byte array to be compressed</Param> 
  22. ///<Returns>Compressed byte array</Returns> 
  23. Public static byte [] CompressByte (byte [] data)
  24. {
  25. MemoryStreamMS=NewMemoryStream ();
  26. StreamS=NewGZipOutputStream (MS );
  27. S. Write (data, 0, data. Length );
  28. S. Close ();
  29. Return ms. ToArray ();
  30. }
  31.  
  32. /**////<Summary> 
  33. /// Decompress the data
  34. ///</Summary> 
  35. ///<Param Name="Data">Byte array to be decompressed</Param> 
  36. ///<Returns>Decompressed byte array</Returns> 
  37. Public static byte [] DeCompressByte (byte [] data)
  38. {
  39. Byte []WriteData=NewByte [2, 2048];
  40. MemoryStreamMS=NewMemoryStream (data );
  41. StreamSm=NewGZipInputStream (MS) as Stream;
  42. MemoryStreamOutStream=NewMemoryStream ();
  43. While (true)
  44. {
  45. IntSize=Sm. Read (writeData, 0, writeData. Length );
  46. If (size>0)
  47. {
  48. OutStream. Write (writeData, 0, size );
  49. }
  50. Else
  51. {
  52. Break;
  53. }
  54. }
  55. Sm. Close ();
  56. Byte []OutArr=OutStream. ToArray ();
  57. OutStream. Close ();
  58. Return outArr;
  59. }
  60. }
  61. }

The above describes ASP. NET ViewState technology.

  1. DataList and Repeater controls of ASP. NET
  2. Analysis of IIS ing in ASP. NET
  3. Overview ASP. NET status types
  4. Introduction to ASP. NET and Web servers
  5. EnableViewState attribute of ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.