Monday, February 1, 2010

What is View State?

The HTTP protocol is a stateless protocol. Each time we request a web page from a website, we are a completely a new person.

The ASP.NET Framework, however, manages to transcend this limitation of the HTTP protocol ASP.NET Framework uses a trick called View State. For example, if we assign a value to Label control’s Text property, the Label control retains this value across multiple page requests.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIxMDI3MzgyODNkZJzgJL2eDEmaIufBUIMhS/GE1lZs" />

This hidden form field contains the value of the Label control’s Text property (and the values of any other control properties that are stored in View State). When the page is posted back to the server, the ASP.NET Framework rips apart this string and re-create the values of all the properties stored in View State. In this way, the ASP.NET Framework preserves the state of control properties across postbacks to the web server.

By default, View State in enabled for every control in the ASP.NET Framework. If you change the background color of a Calendar control, the new background color is remembered across postbacks. The values of these properties are automatically stored in View State.

No comments:

Post a Comment