ASP. net2.0 control status and view status 2

Source: Internet
Author: User

Code Discussion

The implementation of the indexbutton control illustrates three tasks. You must execute these three tasks to make the control participate in the control status:

· Override the oninit method and call the registerrequirescontrolstate method to register with the page to participate in the control status. This task must be completed for each request.

· Override the savecontrolstate method to save data in the control state.

· Override the loadcontrolstate method to load data from the control state. This method calls the base class method and obtains the Base Value of the base class to the control status. If the indexvalue field is not zero and the control status of the base class is not empty, the pair class can be used as a convenient data structure to save and restore the control status composed of two parts.

Analysis Summary

According to a series of technical references on msdn, controlState should be mainly used on custom controls. the net page framework provides the controlState attribute as a method for storing custom control data during server round-trip. "This is the original sentence on msdn, Asp. net2.0 only provides a foundation for controlState. When controlState is a custom state persistence mechanism, that is to say, the State persistence mechanism needs to be completed by your developers, rather than viewstate, it has its own default status persistence mechanism. The use of controlState in a custom control may be Microsoft's intention, so as to avoid the normal operation of the custom control after viewstate is disabled at the page level. Of course, this means that the correct running of some controls depends on its status information. In ASP. net1.1, if viewstate is disabled, the controls cannot run correctly. However, controlState is different after it is introduced, because controlState cannot be disabled.

So Microsoft reminded developers that "Please use the control status only for a small amount of key data that is critical to the control during the sending-back process, instead of using the control status as an alternative option for view status ". Clearly, controlState and viewstate are two things. Although they can accomplish the same task, the new controlState is neither used to replace viewstate nor to replace viewstate. Its mission is to make up for the tasks that cannot be completed by viewstate, so that developers can develop more robust controls. For example, a State of a developed custom control is crucial. Without it, the custom control cannot work normally, so the controlState should be on the stage. In addition, controlState is a custom state persistence mechanism that limits the use of controlState. You should not only register the oninit method and call the registerrequirescontrolstate Method to the page, but also rewrite saveadaptercontrolstate (), the loadadaptercontrolstate (object state) method implements what to save and how to save it. According to my understanding, if you need to save 10 different states of the control, you have to save them one by one and load them one by one. From this point, we can see the original intention of Microsoft. Isn't that obvious? If you don't need controlState, don't use it. Otherwise, how can we let our developers do everything?

This is just the foundation. As I said just now, it seems that Microsoft also said so. controlState is for custom controls. In fact, we really want to enable the controlState of basic controls, such as the label control, microsoft also allows it. This is a bit deeper, and it involves the control adapter (controladapter ). For more information, see use the control adapter to enable the controlState of the basic control. Http://sifang2004.cnblogs.com/archive/2006/06/01/415288.html

Appendix

To fully understand the above content, you must have an understanding of the following:

Pair class

Used to store the basic structure of two related objects. It is processed throughout ASP. NET (such as during Page Status management tasks or in configuration sections) Program There are a variety of usage Utility Classes. In your own code, you can use the pair class to include any location of the structure of two related objects and locations that do not necessarily require data binding. The pair class does not encapsulate its object references first and second in the attribute. This class exposes them as common class fields to all call codes.

The pair class has multiple usage methods in page state retention implementation. The most common usage is to serve as a container for both viewstate and controlState sets. In this case, the first attribute is used for viewstate, while second is used for controlState.

Pagestatepersister class

HTTP requests and responses are originally stateless. To maintain status information between HTTP requests, ASP. NET Server pages can store the page status. This state is called a view State. It contains page and control settings and data, which make pages and controls look like the last time they were submitted to the server and then returned to the client, the pages and controls that the user sees and interacts with are the same. There are several mechanisms to store view statuses between consecutive requests on the same page. The pagestatepersister abstract class indicates the base class of the state information storage mechanism.

To retain the view State on a client that does not support the existing view State persistence mechanism, you can extend the pagestatepersister class, introduce your own view State persistence method, and use the page adapter to ASP.. NET applications are configured to use different view State persistence mechanisms based on the client type that provides pages for them. Classes derived from the pagestatepersister class must override the Save abstract method to store view and control statuses in persistent media, and override the load method to extract status information. If you want to know how to write the derived class of pagestatepersister, see view State persistence mechanism.

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.