Discussion on control state and view state of ASP.net 2.0

Source: Internet
Author: User
Tags html form

Basic concepts

Control state-Sometimes you need to store control state data in order for the control to work properly. For example, if you write a custom control that has different tabs for displaying different information, the control needs to know which tab is selected during a round trip to make the control work as expected. The ViewState property can be used for this purpose, but the developer may have turned off view state at the page level to effectively break the control. To resolve this issue, the ASP.net page framework exposes a new feature called control state in ASP.net version 2.0.

The ControlState property allows control-specific property information to be persisted, not as closed as the ViewState property. To use control state, the control must call the Registerrequirescontrolstate method during initialization and then override the SaveControlState and LoadControlState methods.

View state-view state is the method by which the ASP.net page framework saves page and control values between round trips by default. When the HTML form of the page is rendered, the current state and value of the page that needs to be retained during postback are serialized as BASE64 encoded strings and exported to the hidden fields in view state. By implementing a custom PageStatePersister class to store page data, you can change the default behavior and store view state in another location, such as a SQL Server database. For an example of storing page state on a stream rather than a hidden page field, see Examples of view state persistence mechanisms.

You can use your own code to access view state by using the page's ViewState property to save data from a round trip to a WEB server. The ViewState property is a dictionary that contains a key/value pair that contains view-state data.

Their strengths and weaknesses

View state

Advantages of using view state:

• You do not need any Server resource view state to be included in the structure within the page code.

• Implement simple view state without using any custom programming. The maintenance of state data is enabled for the control by default.

• Enhanced security features the values in view state are hashed and compressed and encoded for UNICODE implementations, which are more secure than using hidden fields.

Disadvantages of using view state

• Performance considerations Because view state is stored in the page itself, if a large value is stored, the user may slow down when the page is displayed and when the page is sent. Especially for mobile devices, the bandwidth is usually limited.

• Device throttling mobile devices may not have enough memory capacity to store large amounts of view-state data.

• Potential security risk view state is stored in one or more hidden fields on the page. Although view state stores data in hashing format, it can be tampered with. If you view the page output source directly, you can see the information in the hidden field, which causes potential security problems.

control state

Advantages of using control state:

• No server resources are required by default, control state is stored in hidden fields on the page.

• Reliability because control state is not as close as view state, control state is a more reliable way to manage the state of a control.

• Versatility a custom adapter can be written to control how storage location of control state data and control state data is stored.

Disadvantages of using control state:

• Need some programming although the ASP.net page framework provides the basis for control state, the control state is a custom state retention mechanism. To take full advantage of control state, you must write code to save and load control state.

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.