Viewstate in ASP. NET
Viewstate is a mechanism used in ASP. NET to save the status value when the web control is returned. When the web form is set to runat = "server", this form is appended with a hidden attribute _ viewstate. _ Viewstate stores the State values of all controls in viewstate.
Viewstate is a field in the control class. All other controls inherit control to obtain the viewstate function. Its type is system. Web. UI. statebag, an object set of name/value.
When requesting a page, ASP. NET serializes the status of all controls into a string and sends it to the client as a hidden property of the form. When the client transfers the page back, ASP. NET analyzes the form attributes returned by the return and assigns them to the corresponding values of the control. Of course, these are all handled by ASP. NET, which is transparent to users.
Conditions for using viewstate
To use viewstate, you must have a server-side form mark () on the ASPX page (). Form fields are required so that hidden fields containing viewstate information can be returned to the server. In addition, this form must also be a form on the server. When the server executes this page, the ASP. NET page framework can add hidden fields.
The value of enableviewstate in page is true.
The value of the enableviewstate attribute of the control is true.
The page itself stores about 20 bytes of information in viewstate, which is used to distribute the PostBack data and viewstate values to the correct control during back-to-transmission. Therefore, even if the page or applicationProgramIf viewstate is disabled, a small number of remaining bytes can still be seen in viewstate.
Set viewstate
Viewstate can be set in controls, pages, programs, and global configuration. By default, enableviewstate is true. To disable the viewstate function for all pages, set enableviewstate to false in program configuration.
In the control:
Or
Datagrid1.enableviewstate = false;
On the page:
Or
Page. enableviewstate = false;
In the program:
Add in Web. config
In global Configuration:
Modify in machine. config
Enableviewstate priority:
Global configuration <Program <page <Control
Note: viewstate cannot be disabled for the following server controls
Textbox
Checkbox
Checkbox list
Radiobuttonlist
The status of the above control is processed through the ipostbackeventhandler and ipostbackdatahandler interfaces, rather than the viewstate mechanism, so enableviewstate has no effect.
Viewstate object
In the communication between page backhaul, ASP generally uses form attributes and sessions to store data. ASP. NET can also use viewstate objects for the same processing.
Store data in viewstate:
Viewstate [Key] = value;
Or
Viewstate. Add (Key, value );
Retrieve Data:
Tempstr = viewstate [Key];
If the key does not exist, null is returned.
The value of the control cannot be accessed through the viewstate object.
Dynamically create the viewstate of the control:
When a server control needs to be dynamically created, a radiobutton control is created as follows and added to the form control set:
Radiobutton RB = new radiobutton ();
Page. controls [1]. Controls. Add (PC );
The aboveCodeAdd a control to the end of the control set. You can also insert it to any position in the existing control.
Radiobutton RB = new radiobutton ();
Page. controls [1]. Controls. addat (1, PC );
Generally, the status of these dynamically generated controls also needs to be generated into viewstate, but this function is not fully implemented, especially when the generated control is inserted into an existing control.
The result of viewstate is unpredictable when both the dynamically generated control and the existing control coexist. When the page is returned, the non-dynamically generated control is first generated on the ASPX page, and
And read viewstate in the page_load event. When the page control reads the value of viewstate, those dynamically generated controls are not yet generated, so when the dynamically generated controls are
When the viewstate is generated, the page will omit the viewstate or fill the control with the remaining or incorrect viewstate.
Therefore, when you need to insert a dynamically generated control to an existing control, it is best to disable the viewstate of this control through enableviewstate.
Reminder:
1. When page callback exists, viewstate is disabled if you do not need to maintain the value of the control.
2. viewstate indexes are case sensitive.
3. viewstate is not cross-page.
4. To ensure that the package can exist in viewstate, the object must be streamed or typeconverter defined.
5. When the textmode attribute of the control textbox is set to password, its status will not be saved in viewstate, which should be for security consideration.
6. Do not use viewstate when the page is not returned or redirected or when it is redirected to another page (transfer.
7. Be careful with its viewstate when creating a control dynamically.
8. When viewstate of a program is disabled, the viewstate of all pages of the program is also disabled.
9. viewstate is continuous only when the page returns itself.