NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

View State Management

The core HTTPRuntime infrastructure of ASP+ provides a collection of server-only state management services that enable you to store state information at either an application or user session level (see other specifications for details). The Web Forms Page framework augments these options by enabling page and control developers to also persist view state information (state associated with a particular view of an HTML page).

View state information is persisted using the Control.State dictionary. This instance of the ViewStateBag class automatically maintains a localized dictionary view of view state data, which means two controls on the same page could persist a value with a common key without colliding (the page framework uses a control’s UniqueID to avoid conflicts).

You can create code to read from the State collection at any time during a page request (although values cannot be written into the collection during or after the Render portion of page execution). The best way to accomplish this is by creating properties that represent the values you want to persist and read or write the current state of the values using get and set.

The following example, which creates the new class Table, shows how to read and write the BGColor item in the State collection and make it available programmatically through the property.BGColor.

public class Table : Control {

   // Expose a public "BGColor" property on the table control.
   //Store the value in view state to ensure that the value is 
   //automatically round-tripped back to the server on postback.

   public String BGColor {
      get {
         return State["BGColor"];
      }
      set {
         State["BGColor"] = value;
      }
   }
}

At render time, the collective view state of all server controls on a page is output by server-side form controls into a specially named hidden form variable (__VIEWSTATE) on the generated HTML page.