Taking a Bite out of asp.net viewstate

zhaozj2021-02-11  167

Taking a Bite Out of ASP.NET ViewStateasp.net ViewState brief introduction

Susan Warrenmicrosoft Corporation

NOVEMBER 27, 2001

When I meet and talk to new ASP.NET page developers, one of the first things they usually ask me is, "What is that ViewState thing, anyway?" And often you can hear in their voices that same queasy fascination I feel when a waiter in some exotic restaurant parks a plateful of some previously unknown food in front of me Somebody must think it's good;.. otherwise, they would not be serving it So, I'll try it, and maybe even love it, but it Sure Looks ODD!

It's that way with ViewState too. Once you get past how it looks, you'll find many circumstances where you'll be delighted to have ViewState in your ASP.NET application, because it lets you do much more with much less code. But There Will Also Be Times When You'll Want To Definitely Leave ViewState on The Plate. We'll Look At Both Scenarios, Butfirst, Let's Answer That Questions What is Viewstate.

Answer: ViewState Maintains The Ui State of A Page

The Web is stateless, and so are ASP.NET Pages. They are instantiated, executed, rendered, and disposed on every round trip to the server. As a Web developer, you can add statefulness using well-known techniques like storing state on the Server in session state or by posting a page back to itself. Take The Sign Up Form in Figure 1 as an example.

Figure 1. Restoring Posted Form Values

You can see I've picked an invalid value for my potluck item. Like most forms on the Web, this one is friendly enough to put a helpful error message and a star next to the field in error. In addition, all of the valid values ​​I entered in the other text boxes and drop-down lists still appear in the form. This is possible, in part, because HTML form elements post their current values ​​from the browser to the server in the HTTP header. You can use ASP. Net Tracing to See the Form Values ​​That Are Posted Back, AS in Figure 2.figure 2. Values ​​Posted in Http Form, As Shown by ASP.NET TRAC

Before ASP.NET, restoring the values ​​back into the form fields across multiple postbacks was entirely the responsibility of the page developer, who had to pick them out, one-by-one, from the HTTP form, and push them back into the fields , AUTOTINATING Both A Lot of Grunt Work and a Lot of Code for Forms. But That Not ViewState.

ViewState is the mechanism ASP.NET uses to keep track of server control state values ​​that do not otherwise post back as part of the HTTP form. For example, the text shown by a Label control is saved in ViewState by default. As a developer , you can bind data or programmatically set the label just once when the page first loads, and on subsequent postbacks, the label text will be repopulated automatically from ViewState. So in addition to less grunt work and less code, the benefit of ViewState is often Fewer Trips to the Database.

How ViewState Works

There's really nothing magical about ViewState. It's a hidden form field managed by the ASP.NET page framework. When ASP.NET executes a page, the ViewState values ​​from the page and all of the controls are collected and formatted into a single encoded string, and then assigned to the value attribute of the hidden form field (specifically,). Since the hidden form field is part of the page sent to the client, the ViewState value is temporarily stored in the client's browser. If the client chooses to post the page back to the server, the ViewState string is posted back too. You can actually see the ViewState form field and its postback value in Figure 2 above.Upon postback, the ASP.NET page framework parses the ViewState string and populates the ViewState properties for .

There is Three Small, But Useful Things to Know About ViewState.

You Must Have A Server-Side Form Tag

Getting more from viewstate

ViewState is a marvelous way to track the state of a control across postbacks since it does not use server resources, does not time out, and works with any browser. If you are a control author, you'll definitely want to check out MAINTAING State in a control.

Page authors can also benefit from ViewState in much the same way Occasionally your pages will contain UI state values ​​that are not stored by a control You can track values ​​in ViewState using a programming syntax is similar to that for Session and Cache..:

[Visual Basic]

'save in viewstate

ViewState ("Sortorder") = "DESC"

'Read from ViewState

Dim Sortorder As String = CSTR (ViewState ("Sortorder") [C #]

// Save in viewState

ViewState ["Sortorder"] = "DESC";

// read from viewstate

String Sortorder = (String) ViewState ["Sortorder"];

Consider this example: you want to display a list of items in a Web page, and each user wants to sort the list differently The list of items is static, so the pages can each bind to the same cached set of data, but the. Sort ORDER IS A SMALL BIT OF User-Specific UI State. ViewState Is A Great Place To Store Type of Value. Here's The Code:

[Visual Basic]

<% @ Import namespace = "system.data"%>

Storing Non-Control State in ViewState

This Example Stores The Current Sort Order for A Static Sort ORDER

List of data in viewstate.

Click The link in the column header to sort the data by That field.

Click The Link a Second Time To Reverse The Sort Direction.