I have discussed several blogs about viewstate: "objectively view the role and impact of viewstate on ASP.net programs", validation of viewstate Mac failed exception.. Today, I want to summarize from my current experience that it is reasonable to control viewstate.
Viewstate is an original design of ASP. NET. It brings great convenience to users in Web development. Especially for a new Web Developer, it does not have to maintain the status of Web controls. During event-driven development, the user only obtains the value of the current control through the control ID, and other details ASP. net is transparent to users, so it is very easy to get started. The negative effects it brings are also obvious, and sometimes it is also serious. The most serious is that the execution of the above-mentioned re-sending event is very slow, and sometimes"Validation of viewstate Mac failed"You will not be surprised.
How can we control and use viewstate? I think it is best to disable it, but it is not always the case. You can decide whether to disable viewstate based on your application. What are the factors that decide?
1) Your production environment, that is, where you use it. If you are in a LAN or fiber to the desktop (or you do not need fiber, the speed is guaranteed. Here is just a metaphor ). Then you can safely use viewstate.
2) your understanding of web development. This is a subjective factor. If you have sufficient control capabilities to disable viewstate, you can safely disable viewstate globally in Web. config.
3) If you can accept viewstate subjectively, you hope to make full use of the convenience it brings to us. In addition, you can control viewstate well to prevent it from becoming your performance bottleneck.
4) In your application scenarios, for example, if you are a website front-end, disable viewstate as much as possible. (Of course, it is best not to use PostBack)
There may be other reasons to decide whether to use viewstate. Here we still hope to summarize the convenience that viewstate brings to us, and what by-products it brings to us:
1) The most intuitive is the page size. When viewstate is used, it maintains the status of all controls, and restores the status of controls in a timely manner during PostBack. What is this concept? Is the value of the control (for example, all the options in dropdownlist), in addition to HTML display to the outdoor, it also has a backup in viewstate, this backup will exist in the HTML code sent by the server as an implicit variable, and the viewstate will be sent back to the server at the same time. The page size is virtually doubled.
2) network transmission is naturally increased when the volume is increased. Of course, the response speed of the page decreases.
3) added some additional processing time on the server. Viewstate increases the volume, so the serialization and deserialization of these viewstate values cannot be underestimated. In addition, when restoring the control status, you need to traverse and assign values to the control. This time is also a relatively large overhead.
Disabling viewstate solves some problems. However, as part of the webform lifecycle, The viewstate processing function still needs to be called, but after judging enableviewstate = false, it does less, this saves time. If you carefully check the page for disabling viewstate, you will also find the hidden Variable _ viewstate. If you want to make this implicit variable disappear, you can rewrite the pagestatepersister of the page class to achieve this purpose. However, based on my experience, it seems that this will have some other effects in the future. Therefore, we recommend that you do not do this as much as possible (in fact, it is not necessary to do this ).
If viewstate is disabled, doesn't it mean we can't use viewstate to bring convenience to me? This is definitely not the case. Because viewstate is disabled, we can "Abuse" viewstate. In some cases, we need to use the same value in different controls on the same page. For example, after you query data, the total number of pages must be used in some other controls on the same page. In this case, different controls are not in the same class and cannot be passed using variables. If you put them in the cache and Session, the conflict and expiration time are not under your control. In this case, we can store it in viewstate, because the viewstate scope is limited to this request on this page, and the page output will be recycled with the page instance. Because viewstate is disabled, the value stored in viewstate is not serialized to the client. Such abuse will bring great convenience and flexibility to our development.
The above is my summary of viewstate. Some of the points may be unclear and repetitive. please correct me.
Author: A bu