ViewState in ASP.NET

xiaoxiao2021-03-06  115

ViewState is a mechanism for saving a WEB control back post status value in ASP.NET. In the Web Form (Form) is set to runat = "server", this form (form) will be attached to a hidden property _ViewState. _ViewState stores all controls in ViewState in ViewState.

ViewState is a domain in CONTROL, and all other controls get the ViewState feature by inheritance Control. Its type is System.Web.ui.Statebag, a name / value of an object collection.

When a page requests a page, ASP.NET seizes all controls into a string, and then be sent to the client as the hidden property of the form. When the client returns the page, the ASP.NET analyzes the return form attribute and assigns the value corresponding to the control. Of course, these are responsible for ASP.NET, which is transparent to the user.

Conditions using ViewState If you want to use ViewState, you must have a server-side form mark () in the ASPX page. The form field is required so that the hidden field containing ViewState information can pass back to the server. Moreover, the form must also be a server-side form, so that when the page is executed on the server, the ASP.NET page framework can add hidden fields. Page's enableViewState property value is True. The EnableViewState property value of the control is True.

The page itself saves 20-byte information in ViewState to send the postback data and the ViewState value to the correct control at the time of return. Therefore, even if the page or application is disabled, you can still see a small amount of remaining bytes in ViewState.

Set upstate

ViewState can set in controls, pages, programs, global configurations. By default EnableViewState is True. If all page viewState features are prohibited, you can set EnableViewState to false in the program configuration.

In the control:

or

DataGrid1.enableViewState = false; on page:

<% @ PageenableviewState = "false"%> or

Page.enableviewState = false; in the program:

Add to Web.Config

in global configuration:

Modify in Machine.Config

EnableViewState Priority: Global Configuration

Note: The following server controls cannot disable ViewState

The status of the TEXTBOX CHECKBOX CHECKBOX LIST RADIOBUTTONLIST The status of the above control is handled by IPostBackeventHandler and the iPostBackDataHandler interface, not the mechanism of ViewState, so enableViewState has no effect.

ViewState object

In-page communication, ASP usually uses the form of the property and session to store data, and you can also use the ViewState object in ASP.NET to do the same processing.

Store data in ViewState: ViewState [key] = value; or

ViewState.Add (key, value); Removed data:

Tempstr = viewState [key]; return empty when it does not exist.

You cannot access the value of the control through the ViewState object.

Dynamically establish the viewState of the control:

A server control is required to create a Radiobutton control, and add it to the form control collection:

Radiobutton RB = New Radiobutton (); page.controls [1] .controls.add (pc); The above code adds a control to the control collection, and can also insert anywhere in the existing control.

Radiobutton RB = New Radiobutton (); page.controls [1] .controls.addat (1, pc);

Typically, the status of these dynamically generated controls also needs to be generated into the ViewState, but this feature is not fully implemented, especially when the generated control is inserted into the existing control.

The result of ViewState is unpredictable when dynamically generating controls and existing controls. At the time of the page, the first non-dynamic generated control is generated in the ASPX page and reads ViewState in the Page_init and Page_Load events. When the control of the page reads ViewState, those dynamically generated controls have not been generated, so when the dynamically generated control is generated, the page will omit the viewState or fill the control with the remaining ViewState.

So, when you need to insert a dynamically generated control to the existing control, it is best to prohibit this control via EnableViewState.

Reminder: 1. When there is a page back post, you don't need to maintain the control, you will prohibit the viewState. 2. VIEWSTATE index is sensitive. 3. ViewState is not a cross page. 4. In order to be able to include viewState, the object must be fluidized or defined TypeConverter. 5. When the TEXTBOX's TextMode property is set to Password, its status will not be saved in ViewState, which should be considered for security. 6. Do not use ViewState when there is no return or redirect or redirect or transfer to (TRANSFER) other pages. 7. Be careful when building a control is dynamically established. 8. When a program is prohibited, the ViewState of all pages of this program is also banned. 9. ViewState is only continuous only when the page is back in itself.

转载请注明原文地址:https://www.9cbs.com/read-126300.html

New Post(0)