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 Services

The HTTPRuntime infrastructure provides server-side state management services that allow web developers to store state information at either an application or user session level. The ASP+ framework augments the HTTPRuntime state services by allowing page and control developers to persist view state, the state associated with a particular view of a page.

Page and control developers can persist view state by storing and retrieving values to/from the Control.ViewState dictionary, an instance of the ViewStateBag class. The Control.ViewState dictionary automatically maintains a localized "picture" of view state data. Page and control developers can read from the ViewState collection at any time during a page request, but cannot write values into the ViewState dictionary during or after the Render stage of the page lifecycle. For information on the Render stage, see Writing a Custom Web Forms Control.

The following example shows how a control developer can persist view state by storing a public property in the control's ViewState dictionary:

public class Table : Control {
// Expose a public "BGColor" property on the table control.
// Store the value in viewstate to ensure that the value is
// automatically round-tripped back to the server.
   public String BGColor {
      get {
         return ViewState["BGColor"];
      }
      set {
         ViewState["BGColor"] = value;
      }
   }
}

At render time, all server controls on a page that store view state in the ViewState dictionary write that information into a specially named hidden form variable (__VIEWSTATE) on the dynamically generated HTML page:

<form>
   <input type=hidden name="__VIEWSTATE"
      value="<a><s>BgColor:red</s></a>">
   <input type=hidden name="__VIEWSTATEMAC" value=”434343433433”>

   <table bgcolor="red"> </table>
</form>

The __VIEWSTATEMAC hidden form variable supports a Message Authentication Check (MAC) on page postback for security purposes. If the MAC of the incoming view state data is legal, the framework automatically populates the ViewState dictionary with its previously saved state data immediately prior to calling the Init method on each server control.

See Also

ASP+ Web Forms