View State and Control State


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 State

You 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

Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace myControls     Public Class ViewStateControl         Inherits WebControl         Private _text As String         Public Property Text() As String             Get                 Return _text             End Get             Set(ByVal Value As String)                 _text = value             End Set         End Property         Public Property ViewStateText() As String             Get                 If IsNothing(ViewState("ViewStateText")) Then                     Return String.Empty                 Else                     Return CType(ViewState("ViewStateText"), String)                 End If             End Get             Set(ByVal Value As String)                 ViewState("ViewStateText") = value             End Set         End Property         Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)             writer.Write("Text: " & Text)             writer.WriteBreak()             writer.Write("ViewStateText: " & ViewStateText)             writer.WriteBreak()         End Sub     End Class End Namespace 

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

<%@ Page Language="VB" %> <%@ Register TagPrefix="custom" Namespace="myControls" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     sub Page_Load()         If Not Page.IsPostBack Then             ViewStateControl1.Text = "Hello World!"             ViewStateControl1.ViewStateText = "Hello World!"         End If     end sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show View State</title> </head> <body>     <form  runat="server">     <div>     <custom:ViewStateControl                  Runat="server" />     <asp:Button                  Text="Submit"         Runat="server" />     </div>     </form> </body> </html> 

Supporting Control State

The 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

Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace myControls     Public Class ControlStateControl         Inherits WebControl         Private _controlStateText As String         Public Property ViewStateText() As String             Get                 If IsNothing(ViewState("ViewStateText")) Then                     Return String.Empty                 Else                     Return CType(ViewState("ViewStateText"), String)                 End If             End Get             Set(ByVal Value As String)                 ViewState("ViewStateText") = Value             End Set         End Property         Public Property ControlStateText() As String             Get                 Return _controlStateText             End Get             Set(ByVal Value As String)                 _controlStateText = Value             End Set         End Property         Protected Overrides Sub OnInit(ByVal e As EventArgs)             Page.RegisterRequiresControlState(Me)             MyBase.OnInit(e)         End Sub         Protected Overrides Function SaveControlState() As Object             Return _controlStateText         End Function         Protected Overrides Sub LoadControlState(ByVal savedState As Object)             _controlStateText = CType(savedState, String)         End Sub         Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)             writer.Write("ViewStateText: " + ViewStateText)             writer.WriteBreak()             writer.Write("ControlStateText: " + ControlStateText)             writer.WriteBreak()         End Sub     End Class End Namespace 

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

<%@ Page Language="VB" %> <%@ Register TagPrefix="custom" Namespace="myControls" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">     sub Page_Load()         If Not Page.IsPostBack Then             ControlStateControl1.ViewStateText = "Hello World!"             ControlStateControl1.ControlStateText = "Hello World!"         End If     end sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Control State</title> </head> <body>     <form  runat="server">     <div>     <custom:ControlStateControl                  EnableViewState="false"         Runat="server" />     <asp:Button                  Text="Submit"         Runat="server" />     </div>     </form> </body> </html> 




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net