ViewState in ASP.NET

zhaozj2021-02-17  56

ViewState in ASP.NET

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 tag (

) 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.

SetupStateViewState to 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:

<% @ Page EnableViewState = "false"%>

or

Page.enableViewState = false;

In the program:

Join in Web.config

In the global configuration:

Modify EnableViewState priority: Global Configuration

Note: The following server controls cannot disable ViewState

TextBox CheckBoxCheckbox ListradiobuttonList

The status of the above control is handled by IPostBackeventHandler and the iPostBackDataHandler interface, not the mechanism of ViewState, so EnableViewState has no effect.

The ViewState object communicates with the page back, ASP usually uses the properties of the form 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);

Take out the data:

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

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

Dynamically set up the control of the control: When you need to create a server control, a Radiobutton control is created as follows 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 end of the control collection, and can also insert anywhere in the existing control.

Radiobutton RB = New Radiobutton (); page.controls [1] .controls.addat (1, pc); usually, these dynamic generated controls also need to generate into ViewState, but this feature is not fully implemented, especially Is the generated control 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 pass, 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.

Finally, I will use the product INET in Stryon (http://www.stryon.com.cn), http://208.49.57.227: 8080 / downloads / default.aspx) Java implements ViewState, there is no difference in use.

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

New Post(0)