Asp. NET in the ViewState

Source: Internet
Author: User
asp.net ASP. NET in the ViewState
ViewState is a mechanism for saving the state value of a Web control postback in asp.net. When the Web form is set to runat= "Server", the form is appended with a hidden property _viewstate. The state value of all controls in the viewstate is stored in the _viewstate.

ViewState is a domain in class control, and all other controls gain viewstate functionality by inheriting it. Its type is System. Web.UI.StateBag, a collection of object names/values.

When a page is requested, the ASP. NET serializes the state of all controls into a string, and then sends the form's hidden properties to the client. When the client is returning the page, the ASP. NET analyzes the properties of the form that are returned and assigns the corresponding value to the control. Of course, all of these are owned by ASP.net, which is transparent to the user.

Conditions for using ViewState

If you want to use ViewState, you must have a server-side form marker (<form runat=server>) in the ASPX page. form fields are required so that hidden fields containing ViewState information can be passed back to the server. Also, the form must be a server-side form, so that the ASP.net page frame can add hidden fields when the page is executed on the server.
The Page's EnableViewState property value is true.
The control's EnableViewState property value is true.

The page itself stores about 20 bytes of information in ViewState, which is used to distribute postback data and ViewState values to the correct control at the time of the postback. Therefore, even if the page or application disables ViewState, you can still see a small amount of remaining bytes in ViewState.

Set ViewState
ViewState can be set in controls, pages, programs, and global configurations. EnableViewState is true by default. If you want to disable all page ViewState features, you can set the EnableViewState to False in your program configuration.
In the control:

<asp:datagrid enableviewstate= "false"%>

Or

Datagrid1.enableviewstate = false;

In the page:

<%@ Page enableviewstate= "false"%>

Or

Page.enableviewstate = false;

In the program:

Add <pages enableviewstate= "false" in Web.config/>

In the global configuration:

Modify <pages enableviewstate= "false" in Machine.config/>

EnableViewState Priority level:
Global Configuration < Programs < pages < controls

Note: The following server controls cannot be prevented from ViewState

Textbox
Checkbox
Checkbox List
RadioButtonList

The state of the control above is handled through the IPostBackEventHandler and IPostBackDataHandler interfaces, rather than the viewstate mechanism, so enableviewstate has no effect.

ViewState objects
In the page return communication, ASP generally use form properties and session to store data, in asp.net can also use ViewState object to do the same processing.
Storing data in ViewState:

Viewstate[key] = value;

Or

Viewstate.add (key, value);

To remove data:

TempStr = Viewstate[key];
Returns NULL when key does not exist.

The value of the control cannot be accessed through the ViewState object.

To dynamically establish the viewstate of a control:
When you need to dynamically build a server control, you set up a RadioButton control and add it to the form control collection:

RadioButton RB = new RadioButton ();
PAGE.CONTROLS[1]. Controls.Add (PC);

The code above adds a control to the end of the control collection and can also be inserted anywhere in the existing control.

RadioButton RB = new RadioButton ();
PAGE.CONTROLS[1]. Controls.addat (1,PC);

Typically, the state of these dynamically generated controls also needs to be generated into the ViewState, but this feature is not fully implemented, especially if the generated control is inserted into an existing control.
The result of ViewState when a dynamically generated control coexists with existing controls is unpredictable. When the page is returned, the first non dynamically generated control is generated in the ASPX page, and the Page_Init
And the Page_Load event to read the ViewState. When a page's control reads a ViewState value, those dynamically generated controls are not yet generated, so when dynamically generated controls are
When generated, the page omits the ViewState or fills the control with the remaining ViewState that may be wrong.

So, when you need to plug a dynamically generated control into an existing control, it's a good idea to disable the control's ViewState through EnableViewState.

Remind:
1. When there is a page return, do not need to maintain the value of the control to ViewState prohibited.
2. The index of the ViewState is case sensitive.
3. ViewState is not a cross page.
4. In order to be able to package exist in ViewState, the object must be either fluidization or defined TypeConverter.
5. When the TextMode property of a control TextBox is set to password, its state will not be saved in ViewState, which should be for security reasons.
6. Do not use ViewState when the page does not return or redirect, or when you go to (transfer) other pages in the back crosses.
7. Be careful with the ViewState of the control when it is dynamically built.
8. When the ViewState of a program is prohibited, the ViewState of all pages of the program is also prohibited.
9. ViewState is persistent only when the page is returned to itself.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.