Working with View State


By default, almost all ASP.NET controls retain the values of their properties between form posts. For example, if you assign text to a Label control and submit the form, when the page is rendered again, the contents of the Label control are preserved.

The magic of view state is that it does not depend on any special server or browser properties. In particular, it does not depend on cookies, session variables, or application variables . View state is implemented with a hidden form field called VIEWSTATE that is automatically created in every Web Forms Page.

When used wisely, view state can have a dramatic and positive effect on the performance of your Web site. For example, if you display database data in a control that has view state enabled, you do not have to return to the database each time the page is posted back to the server. You can automatically preserve the data within the page's view state between form posts.

Disabling View State

In certain circumstances, you might want to disable view state for an individual control or for an ASP.NET page as a whole. For example, you might have a control that contains a lot of data (imagine a RadioButtonList control with 1,000 options). You might not want to load the data into the hidden __VIEWSTATE form field if you are worried that the form data would significantly slow down the rendering of the page.

NOTE

In Chapter 18, "Application Tracing and Error Handling," you learn how to determine how each control affects the total size of a page's view state.


You also might want to disable view state if you need to automatically reinitialize a control every time a page is loaded. For example, through view state, a Label control retains whatever text you assign to it across multiple form posts.

NOTE

Form controls, such as TextBox and RadioButtonList , retain their values between posts even when view state is disabled. The values of these controls do not need to be preserved in the VIEWSTATE hidden form field because they are actually being submitted to the server on each form post.


Consider the page in Listing 4.1. It contains a single Label control and a single Button control. When the page is first loaded, a message is assigned to the Label control's Text property. If you click the button and reload the page, however, the label continues to display the message. The Label control's Text property is automatically preserved through view state.

Listing 4.1 ViewState.aspx
 <Script Runat="Server"> Sub Page_Load   If Not IsPostBack Then     lblMessage.Text = "Hello!"   End If End Sub </Script> <html> <head><title>ViewState.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage"   Runat="Server" /> <p> <asp:Button   Text="Just Submit"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

You can disable view state for an individual control by modifying the EnableViewState property. You can use this property to disable view state for both HTML and Web controls.

The page in Listing 4.2, for example, uses the EnableViewState property to disable view state for a Label control. When the page is first loaded, a message is assigned to the Label control and the message is displayed. If you click the button, however, and reload the page, the text disappears from the label.

Listing 4.2 EnableViewState.aspx
 <Script Runat="Server"> Sub Page_Load   If Not IsPostBack Then     lblMessage.Text = "Hello!"   End If End Sub </Script> <html> <head><title>EnableViewState.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage"   EnableViewState="False"   Runat="Server" /> <p> <asp:Button   Text="Just Submit"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Instead of disabling view state control by control, you also can disable view state for the whole page. You should do so when you are not taking advantage of view state and the controls in a page contain a lot of data. To disable view state for an entire page, modify the EnableViewState attribute of the Page directive. The page in Listing 4.3 demonstrates how you would do so.

Listing 4.3 PageViewState.aspx
 <%@ Page EnableViewState="False" %> <Script Runat="Server"> Sub Page_Load   If Not IsPostBack Then     lblMessage.Text = "Hello!"   End If End Sub </Script> <html> <head><title>PageViewState.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage"   Runat="Server" /> <p> <asp:Button   Text="Just Submit"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Adding Values to View State

You can take advantage of view state within your code by adding values to the state bag class . If you add values to this class, they are automatically added to the hidden VIEWSTATE form variable; therefore, they are available across multiple form posts.

To add a value to the state bag, use a statement such as the following:

 
 ViewState( "SomeItem" ) = "Some Value" 

This statement adds a new item to the state bag class named SomeItem with the value Some Value .

CAUTION

ViewState is case-sensitive. The following two statements are not equivalent:

 
 Response.Write( ViewState( "Count" ) ) Response.Write( ViewState( "count" ) ) 

The page in Listing 4.4, for example, uses the state bag class to record the number of times the button on the form is clicked. After you click the button five times, a message is displayed.

Listing 4.4 StateBag.aspx
 <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   ViewState( "TotalCount" ) += 1   If ViewState( "TotalCount" ) = 5 Then     lblCount.Text = "You clicked 5 times!"   End If End Sub </Script> <html> <head><title>StateBag.aspx</title></head> <body> <form Runat="Server"> <asp:Button   Text="Click Here"   OnClick="Button_Click"   Runat="Server" /> <p> <asp:Label   ID="lblCount"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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