What is view state and use of it?

The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested for the first time versus when the form is posted (sent to the server), which allows you to program accordingly.

How do we ensure view state is not tampered?

Using the @page directive and setting ‘EnableViewStateMac’ property to true.

How do I decide when to turn off a control’s view state?

You can look at the source for a page in your browser. In Internet Explorer, right click the page and select View Source. Within the source for your page, you can see the hidden field that contains the view state for a page, called_VIEWSTATE. If the view state hidden field is quite large, you might want to disable view state for the control.

Explain Session state vs View state.

In some cases, using view state is not feasible. The alternative for view state is session state. Session state is employed under the following situations:

Large amounts of data – View state tends tincrease the size of both the HTML page sent the browser and the size of form posted back. Hence session state is used.
Secure data – Though the view state data is encoded and may be encrypted, it is better and secure if nsensitive data is sent tthe client. Thus, session state is a more secure option.
Problems in serializing of objects intview state – View state is efficient for a small set of data. Other types like DataSet are slower and can generate a very large view state.

Can the view state be encrypted?

The view state can be encrypted by setting EnableViewStateMac to true and either modifying the <machineKey> element in Machine.config to <machineKey validation=”3DES” /> or by adding the above statement to Web.config.

Whether Http maintain states by default?

No. Http is a stateless protocol. In order to maintain the state we need either of the following in the pages.

View state
Query string or Request.Form

What’s the maximum size of a view state?

Maximum size of a viewstate should not be more than 25-30% of the page size.