The standard ASP.NET controls retain the values of their properties across postbacks. For example, if you change the text displayed by a Label control, the Label control will continue to display the new text even if you repeatedly post the page containing the Label control back to the server. The ASP.NET Framework takes advantage of a hidden form field named __VIEWSTATE to preserve the state of control properties across postbacks. If you want your controls to preserve the values of their properties, then you need to add the values of your control properties to this hidden form field. The ASP.NET 2.0 Framework supports two methods of preserving values across postbacks. You can take advantage of either View State or Control State. Supporting View StateYou can use the ViewState property of the Control or Page class to add values to View State. The ViewState property exposes a dictionary of key and value pairs. For example, the following statement adds the string Hello World! to View State: ViewState("message") = "Hello World!" Technically, you can add an instance of any serializable class to View State. In practice, however, you should add only simple values to View State, such as Strings, DateTimes, and Integers. Remember that anything that you add to View State must be added to the hidden __VIEWSTATE form field. If this field gets too big, it can have a significant impact on your page's performance. The control in Listing 31.12 has two properties, named Text and ViewStateText. The first property does not use View State, and the second property does use View State. The value of the ViewStateText property is preserved across postbacks automatically. Listing 31.12. ViewStateControl.vb
Notice that the ViewStateText property uses the Control's ViewState collection to preserve whatever value is assigned to the ViewStateText property across postbacks. When you add a value to the ViewState collection, the value is stuffed into the hidden __VIEWSTATE form field automatically. Warning View State is loaded after the Page InitComplete event, and View State is saved after the Page PreRenderComplete event. This means that you should not attempt to retrieve a value from View State before or during the InitComplete event. You also should not attempt to add a value to View State after the PreRenderComplete event. The page in Listing 31.13 includes the ViewStateControl. The text Hello World! is assigned to both control properties in the Page_Load() handler. However, if you post the page back to itself by clicking the button, only the value of the ViewStateText property is preserved across postbacks. Listing 31.13. ShowViewState.aspx
Supporting Control StateThe ASP.NET 2.0 Framework introduces a new feature named Control State. Control State is very similar to View State. Just like View State, any values that you add to Control State are preserved in the hidden __VIEWSTATE form field. However, unlike View State, Control State cannot be disabled. Control State is intended to be used only for storing crucial information across postbacks. Control State was introduced to address a problem that developers encountered in the first version of the ASP.NET Framework. You can disable View State for any control by assigning the value False to a control's EnableViewState property. Often, this is a very good idea for performance reasons. However, disabling View State also made several controls non-functional. For example, by default a GridView control retains the values of all the records that it displays in View State. If you display 500 database records with a GridView control, then by default all 500 records are stuffed into the hidden __VIEWSTATE form field. To improve performance, you might want to disable View State for the GridView. However, a GridView uses the __VIEWSTATE form field to remember crucial information required for the proper functioning of the control, such as the current page number and the currently selected row. You don't want the GridView to forget this critical information even when View State is disabled. The ASP.NET 2.0 Framework introduces the concept of Control State to enable you to save critical information in the hidden __VIEWSTATE form field even when View State is disabled. Microsoft makes it slightly more difficult to use Control State because they don't want you to overuse this feature. You should use it only when storing super critical information. For example, the control in Listing 31.14 includes two properties named ViewStateText and ControlStateText. View State is used to preserve the value of the first property, and Control State is used to preserve the value of the second property. Listing 31.14. ControlStateControl.vb
Notice that the control in Listing 31.14 overrides the base Control class's OnInit(), SaveControlState(), and LoadControlState() methods. In the OnInit() method, the RegisterRequiresControlState() method is called to indicate that the control needs to take advantage of Control State. The SaveControlState() and LoadControlState() methods are responsible for saving and loading the Control State. Notice that Control State is saved as an object. The object is serialized by the ASP.NET Framework into the hidden __VIEWSTATE form field automatically. The page in Listing 31.15 illustrates the difference between View State and Control State. In the Page_Load() handler, the value Hello World! is assigned to both properties of the ControlStateControl. Notice that the control has View State disabled. However, if you click the button and post the page back to itself, the value of the ControlStateText property is not lost. Listing 31.15. ShowControlState.aspx
|