ViewState anatomy (translation and Notes)

Source: Internet
Author: User
ViewState not what?


1. ViewState is not the value of the control used to recover postbacks.


This is done automatically by matching the variable name of the control in form. This is valid only for controls that were created before the Load event was loaded.


2. ViewState does not automatically re-create any controls that are created dynamically through code.


3. is not used to hold user information. Simply save the control state on this page and not pass between pages.

What is
ViewState?


ViewState is used to track and save state information for controls. Otherwise, this information may be lost because the values do not return with the form or are not in page HTML at all.

The
ViewState holds the changed control properties in the code, binds to any data of the control through code, and any changes that are triggered by user actions and postback.


ViewState also provides a status pack (StateBag), a special set or dictionary (collection or dictionary) that can be used to save and recover arbitrary objects or values through a key.


ViewState Format


the __viewstate hidden field that is saved in the form. It's Base64 encoded, not encrypted!


But it is also possible to encrypt (set enableViewStateMac to use machine key for hashing)


encryption: Set MachineKey authentication, but this must be set at the machine level and requires more resources, so it is not recommended.


Listing 1:viewstate Machine Hash Disabled


Machine.config or web.config: <pages enableviewstatemac= ' false '/>


page level directive: <% @Page enableviewstatemac= ' false '%>


page level Script CODE:PAGE.ENABLEVIEWSTATEMAC = false;


Listing 2:viewstate encryption is Enabled


machine.config: <machinekey validation= ' 3DES ' validationkey= ' * '/>


where the validationkey must be the same across a web-farm setup


also requires the enableViewStateMac property setting to is true


before rendering, ViewState was saved in the Page.savepagestatetopersistencemedium method,


is restored in the Page.loadpagestatefrompersistancemedium method when the postback is made.


These two methods can be easily rewritten to implement saving ViewState into session. This is suitable for small bandwidth occasions,


such as mobile devices by default are session. The code is as follows:


Listing 3:viewstate Saved in Sessions State


protected Override Object LoadPageStateFromPersistenceMedium ()


{


return session["ViewState"];


Related Article

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.