When talking to developers who are new to ASP. NET pages, they usually ask me the first question: "What is the ViewState ?" What they feel in their tone is like what I feel when I come to an exotic restaurant and a waiter serves a dish I have never seen before-both confusing and curious. But some people think it is good, otherwise it will not be provided. So I will try it first, maybe I like it, even though it looks weird!
This is also true for ViewState, But if you adapt to its style, you will find that in many cases, you will be happy with your own ASP. NET application, because it can help you use less code to do more work. However, sometimes ViewState is discarded completely. Next we will explain the two cases separately. However, let's first answer the question about ViewState.
Answer: ViewState is used to maintain the UI status of the page.
Web pages are stateless, and ASP. NET pages are stateless. They are instantiated, executed, rendered, and processed during each round-trip to the server. As a Web developer, you can use well-known technologies (such as storing the status on the server in session state or uploading the page back to itself) to add states. The following example describes the registration form in Figure 1.
Figure 1: Restore the form value returned
We can see that I chose an invalid value for the light meal. This form is as friendly as most forms on the Web. It displays a useful error message and an asterisk next to an error field. The form also displays all valid values that I have entered in other text boxes and drop-down lists. This is possible to some extent, because the HTML form Element sends its current value from the browser to the server in the HTTP header. You can use ASP. NET trace to view the return form value, as shown in figure 2.
Figure 2: returned value in the HTTP Form (displayed through ASP. NET tracking)
In ASP. before. NET, it is entirely the responsibility of page developers to restore the value to the form field through multiple backhaul requests. They will have to pick up the return values one by one from the HTTP form, then, push it back to the field. Fortunately, ASP. NET can now automatically complete this task, saving developers from a boring job and having to write a lot of code for the form. But this is not ViewState.