Viewstate Performance Optimization

Source: Internet
Author: User
If you view the HTML of the current browser page in IE Source code You may often see the following Code Parts:

<Input type = "hidden" name = "_ viewstate" value = "ddwtmzu5nzuymtq1o3q8o2w8atwwpjs + o2w8ddw7bdxp PDA + oz47bdx0pdtspg

......

Smart, you will certainly ask, what is this? What is the function? It corresponds to this article Article What is the turning point of intimacy? Let me hear from you.

In fact, this is the characteristic performance of Ms's application of viewstate Technology in Asp.net. In order that the page can still read the original status data of the server control after PostBack, Asp.net uses the 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 <% @ page 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, then Ms architects will not be so cute (what can be left ?), 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 becomes 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 memorystream MS = new memorystream ();

26 stream S = new gzipoutputstream (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 = new byte [1, 2048];

40 memorystream MS = new memorystream (data );

41 stream Sm = new gzipinputstream (MS) as stream;

42 memorystream outstream = new memorystream ();

43 while (true)

44 {

45 int size = 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} Then we load the methods loadpagestatefrompersistencemedium () and savepagestatetopersistencemedium (Object viewstate) in the page control base class derived from the page class. The Code is as follows:

1 Load & Save viewstate data # region load & Save viewstate data

2 protected override object loadpagestatefrompersistencemedium ()

3 {

4 // read data from your own hidden domain _ smartviewstate

5 string viewstate = request. Form ["_ smartviewstate"];

6 byte [] bytes = convert. frombase64string (viewstate );

7 // call the static method compressionhelper. decompressbyte () provided above to decompress the data

8 bytes = compressionhelper. decompressbyte (bytes );

9 losformatter formatter = new losformatter ();

10 return formatter. deserialize (convert. tobase64string (bytes ));

11

12}

13

14 protected override void savepagestatetopersistencemedium (Object viewstate)

15 {

16 losformatter formatter = new losformatter ();

17 stringwriter writer = new stringwriter ();

18 formatter. serialize (writer, viewstate );

19 string viewstatestring = writer. tostring ();

20 byte [] bytes = convert. frombase64string (viewstatestring );

21 // call the static method compressionhelper. compressbyte () provided above to compress data

22 bytes = compressionhelper. compressbyte (bytes );

23

24 // register a new hidden domain _ smartviewstate, which can be defined by yourself

25 this. registerhiddenfield ("_ smartviewstate", convert. tobase64string (bytes ));

26}

27 # endregion

After the above processing, the source code on the Web output page is type:

<Input type = "hidden" name = "_ smartviewstate" value = "h4siaptponwa/81ce1pbwjb /......

<Input type = "hidden" name = "_ viewstate" value = ""/>

The original value of the hidden field "_ viewstate" is empty. Instead, the new hidden field "_ smartviewstate" we registered is used to store compressed character strings, in this way, the speed-up effect is obvious, combined with our projects, such as dg3g. the original viewstate string value of the home page of COM is about 28 K, and the compressed value is 7 K. The original viewstate string value of acafa.com is about 43 K, and the compressed value is 8 K. Such processing is satisfactory. Of course, if you think it is not thorough enough, you can store the viewstate string in the session, the pressure on the server memory will increase sharply (especially when the traffic volume is large). We recommend that you do not use it easily. If your web server has a memory of 10 Gb or 8 GB, try it. The related modification code is as follows:

Code in the above loadpagestatefrompersistencemedium () method body:

String viewstate = request. Form ["_ smartviewstate"];

To:

String viewstate = session ["_ smartviewstate"]. tostring ();

In addition, the Code in the above savepagestatetopersistencemedium () body is:

This. registerhiddenfield ("_ smartviewstate", convert. tobase64string (bytes ));

To:

Session ["_ smartviewstate"] = convert. tobase64string (bytes );

At last, the above Code and solution all come from vs2003 development practices. If it is appropriate for vs2005, I will not make any commitments. However, if you can gain some benefits from the above solutions, I will be very happy.



Transfer http://www.cnblogs.com/aspsir/archive/2006/08/01/465318.html


Well, I didn't try it in 2005, but it seems that I still need to use fewer server controls. Well, viewstate is always used to dynamically create controls. For details about its optimization and performance, refer:

Http://www.cnblogs.com/oletan/archive/2008/12/01/1345326.html

It's quite complete here. Oh, add it to your favorites first.


Http://faq.csdn.net/read/172163.html viewstate Optimization

Http://blog.csdn.net/zpisgod/archive/2004/11/23/192514.aspx

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.