When talking to the developer who just contacted the ASP.NET page, they usually give me the first question is: "What is the viewstate?" The feeling of their tone, just like I came to a foreign country. The feelings of the waiter, the feeling of the waiter, I have never seen any dishes - I don't understand, but also curious. But there is certain that someone thinks it is good, otherwise it will not provide. So, I will try it first, may like it, although it looks really quirky!
This is also true for ViewState, but if you adapt to its style, you will find that in many cases, you will be happy to use ViewState in your own ASP.NET application, because it can help you use less code to do more jobs. However, sometimes it is completely abandoned for ViewState. Below we will elaborate on both cases, but let us answer what is ViewState.
Answer: ViewState is not status of the UI status Web for maintenance page, and the ASP.NET page is not status, which is instantiated, executed, rendered, and processed during each round-trip process of the server. As a web developer, you can use well-known technologies (such as stored on the server in a session state, or return the page back to itself). Below we are discussed as an example in Figure 1.
Figure 1: Restore the flexible form value
As can be seen from the figure, I chose an invalid value for the lunch meal. This form is as friendly as most forms on the Web, which displays a useful error message next to the error field and an asterisk. Moreover, all values I have entered in other text boxes and drop-down lists are also shown. This is somewhat possible because the HTML form element sends its current value from the browser to the server in the HTTP header. You can use ASP.NET trackers to view the return form value, as shown in Figure 2.
Figure 2: Retrieved value in the HTTP form (display via ASP.NET tracking)
Before ASP.NET, the value is restored to the value of the value to the Form field through multiple times, they will have to pick up the return value from the HTTP form, and then push it back in the field. Fortunately, now ASP.NET can automatically complete this task, so that the developer eliminates an annoying job, but also does not need to write a lot of code for the form. But this is not ViewState.
ViewState is a mechanism, ASP.NET uses this mechanism to track server control status values, otherwise these values will not be sent back as part of the HTTP form. For example, the text displayed by the Label control is saved in ViewState by default. As a developer, you can bind the data, or only set up the Label programming when loading this page. In subsequent return, the label text will automatically refill from ViewState. Therefore, in addition to cumbersome work and code, ViewState can also reduce the number of round trips of the database.
ViewState's working principle ViewState does not have any mystery, which is a hidden form field managed by the ASP.NET page framework. When ASP.NET performs a page, the viewState value and all controls on this page will be collected and formatted into an encoded string, and then assigned to the value attribute of the hidden form field (ie ) . Since the hidden form field is part of the page sent to the client, the ViewState value is temporarily stored in the client's browser. If the client selects the page back to the server, the ViewState string will also be passed back. The viewState form field and its return value can be seen in Figure 2 above. After passing, the ASP.NET page frame will parse the ViewState string and populate the ViewState property for the page and each control. The control then uses ViewState data to restore yourself to the previous state.
About ViewState has three small problems worth noting.
If you want to use ViewState, you must have a server-side form tag (