ASP. NET Server control status (1)

Source: Internet
Author: User

In the ASP. NET Server Control view status article, we discussed the view status content. Some readers have found that if the view status is disabled for a page or control, developers cannot guarantee that the status information stored in ViewState can be properly applied. This is indeed a defect for ViewState. To solve this problem, ASP. NET 2.0 adds a technical feature-ASP. NET Server control status. This article first introduces the basic concepts of the control state, and then describes how to apply the control state through a typical example. Before reading this article and understanding the control status, we recommend that you first read the concept of view chart status in the ASP. NET Server Control view.

ASP. NET Server control status overview

To make the server control work normally, you sometimes need to store the control status data. For example, if you write a custom control with different tabs that display different information, to make the control work as expected, the control needs to know which tab is selected during the round-trip process. ViewState can be used for this purpose. However, developers may disable the view State at the page level to effectively interrupt the control. To solve this problem, ASP. NET 2.0 adds a new feature called "control status.

In general, the control status and view status have many similarities. For example, both of them can be used to store and manage status information, the related data is stored in one or more hidden fields. However, the biggest characteristic of the control status is that the control status cannot be closed, which is completely different from the view status. At the same time, this technical feature is used only for the control range of the server and cannot be used for the Web page range. EnableViewState = "false") when the view status function is disabled on a page or a control, the control status can still be used as usual and will not be affected. In this case, functions related to view status are affected and cannot work. It can be seen that the control status is of great significance in improving the reliability and flexibility of the control.

Similar to the view status, multiple data type objects can be stored in the control status, and the default supported data type range is wider.

The data types include Array, DateTime, Int16, String, ArrayList, Double, Int32, String [], Boolean, Enum, and nullNothing. string. empty, Byte, Hashtable, Pair, Triplet, Char, HybridDictionary, Single, Type, Color, IDictionary.

The method for applying the control status of ASP. NET Server controls is relatively simple. It includes two key processes:

1) OnInit event handling method during initialization) Call the RegisterRequiresControlState method;

2) override the SaveControlState and LoadControlState methods. The former is used to enable and instruct the Server Control to use the control status, and the latter is used to maintain the control status data.

The following uses a simple example to describe how to apply the control status. The Code is as follows:

 
 
  1. PublicClass Sample: Control {
  2. PrivateIntCurrentIndex = 0;
  3. // Rewrite the OnInit event handler
  4. Protected override void OnInit (EventArgs e ){
  5. Page. RegisterRequiresControlState (this );
  6. Base. OnInit (e );
  7. } // Override the SaveControlState Method
  8. Protected override object SaveControlState (){
  9. ReturnCurrentIndex! = 0? (Object) currentIndex:Null;
  10. } // Rewrite the LoadControlState Method
  11. Protected override void LoadControlState (object state ){
  12. If (state! =Null) {CurrentIndex = (Int) State ;}
  13. }
  14. }

As shown in the code above, the custom server Control Sample inherits from the Control, which overwrites three important methods: OnInit, SaveControlState, and LoadControlState.

During the OnInit method rewriting, The RegisterRequiresControlState method of the Page class is called to indicate that the custom control uses the control state, and then the base class method is called. The SaveControlState method is used to save any server control status changes that occur after the page is sent back to the server. The state parameter indicates the Object of the control status to be restored. As shown in the code, this method is rewritten to determine whether the internal property currentIndex is set to a non-default value. If yes, the value is saved to the control status. The LoadControlState method is used to restore the control state information from the previous page saved by the SaveControlState method. As shown in the code, this method is rewritten to determine whether the control status has been saved for the control. If the control status has been saved, set the internal property currentIndex to the saved value.

You must note the SaveControlState and LoadControlState methods. This is a member method added by ASP. NET 2.0 for the Control class. Developers can rewrite these two key methods to manage and control the status data of custom server controls. During the execution of the server control, the SaveControlState method is triggered before the SaveViewState Method for saving the custom view State data. The LoadControlState method is triggered before the LoadViewState Method for loading the custom view State data.
Using the control status has the following advantages:

1. Less server resources are consumed than Application and Session resources ). By default, the control status is stored in the hidden domain on the page.

2. Powerful reliability. Because the control status is not as close as the view status, the control status is a more reliable way to manage the control status information.

3. Have certain flexibility. Developers can write programs to control the storage location of control status data and control status data.

The main disadvantage of using the control status is that some programming is required. Although the ASP. NET page framework provides the foundation for the control status of ASP. NET servers, the control status is a custom state persistence mechanism. To make full use of the control status, developers must write code to save and load the control status.


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.