Monday, April 21, 2008

ViewState versus SessionState

I had previously adopted a practice of using sessionstate to persist data across the stateless web. Unfortunately, this practice carried many unanticipated consequences (such as timing out frequently).

I was recently introduced to the viewstate object, which stores variables in a similar hash-table like configuration. The significant benefit being that the viewstate is stored on the client, and will not timeout as long as the page remains open. Other benefits include the ability to expand to a web-farm web-server arrangement (this is not possible with a server stored variable), and a reduction in memory bloat on the web-server.

There are, of course, drawbacks. The most significant is page performance. This is because the viewstate variables are streamed to the client. This drawback should be reduced with the partial postbacks in the AJAX architecture.

Here is a simple example of storing a viewstate object. Remeber that like the sessionstate, the view state can store any serializable object:
ViewState("tagName") = "this Tag Name"

Dim thisTagName as String = ViewState("tagName")
Response.Write(thisTagName)

No comments: