Written on Thursday, March 21, 2013
The first time I learned the term viewstate, I graduated from school two years later. In school projects, we only focus on the Implementation Effect and do not care about the implementation principle. However, as the study goes deeper, many people feel the same way, that is, they learn more and feel that they do not know anything. Personally, this is actually a transitional period of learning. Of course, people at this stage must be able to withstand loneliness and suffering, so success is just around the corner.
Next, let's take a look at the topic. viewstate is a kind of state Persistence Solution in Asp.net and a client state Persistence Solution.
Speaking of this, let's take a look at the following solutions: What are the main solutions for client-side status persistence and server-side status persistence?
Client status Persistence Solution:
01. viewstate
02. hiddenfield
03. Cookies
04. controlState
05. querystring
There are three client status persistence solutions in essence: Hiding domain/cookies/querystring
Server Status Persistence Solution:
01. Session
02. Application Object
03. caching
04. Database
Today we will mainly discuss viewstate
Viewstate: exclusive to ASP. NET. aspx pages, which belong to page-level objects. That is to say, viewstate can only be used on this page. It is a way to save the data used by the client separately in a hidden domain on the page. in ASP. NET, the values of non-single-value server-side controls are automatically saved in viewstate. To put it bluntly, viewstate can no longer be seen in Chinese on the page. The hidden domain is the container used by viewstate to save the view State.
Because the text box is a single-value control (only one value can be stored), it is not saved to viewstate., The value of the text box can be retained by other State persistence methods. Which controls are not saved in viewstate? In fact, we can sum up in one sentence, that is, the values of elements in a single value form do not use viewstate to save the status. For example, the text box can be post to the server because the value of the text box has the name attribute, so it is not necessary to use viewstate to save the status. The control we see on a daily basis. For example, the State persistence of the gridview is done through viewstate. To put it simply, when the page is sent back, the reason why the data in the gridview still exists is that the viewstate is enabled by default, and the Control name and other information of the gridview are recorded in the viewstate, after a series of processing, the server restores the data to the gridview Based on the recorded gridview Control name and other information, and also makes the same record in the hidden fields of the page.When the client sends a request to the server through post, it carries the viewstate. After the server receives the user request page, the request body automatically finds whether the hidden domain named _ viewstate exists. If yes, decodes the Intermediate Value and adds it to the viewstate attribute of the page.
Prerequisites for using viewstate:
The premise of using viewstate is that there must be a server flag on the page
<Form runat = "server"> otherwise, viewstate cannot be discussed.
Viewstate is applicable to multiple interactions with the server when the same page is not closed.
Overview of viewstate-related events
Let's start with the page lifecycle.
Page lifecycle (part of the page lifecycle ):
1. Create a page Class Object
2. Call the processrequest method of the page class.
3. loadallstate (): Load view status
4. Load
5. saveallstate (): Save the view status.
6. Render () method, display control
Whether it is boring to read so many texts. Let's take a look at the figure below.
Graphical viewstate
ArticleFinally, let's look at how to disable viewstate
Viewstate disabling method:
1,
Enableviewstate = false is disabled for a single viewstate. textbox is not affected after viewstate is disabled because the value of input does not depend on viewstate.
2,
Disable the viewstate of the entire page and add enableviewstate = "false" to the page instruction area of Aspx ".
3,
When some properties of some controls do not fall within the submission range of browser forms,. NET Framework adds these properties to viewstate and saves them.
4,
Webform ispostback is implemented based on _ viewstate.
Now, we will discuss viewstate here. I also hope laruence will give valuable comments on my article knowledge points. After all, we have limited capabilities. Welcome to the discussion.