What is ViewState ?
ViewState is used to store the 'state' of the object in a hidden field on the page. ViewState is sent to the client browser and back to the server, and is not stored on the server. ViewState is used the retain the state of server-side objects between postabacks.
How to use ViewState?
To use the ViewState, you do not need to do anything specifically. Unless you disable it, the ASP.NET framework will add a hidden filed called __VIEWSTATE to your web page. When displayed on a browser, you can right click on the page and view the source. The source will show an entry similar to shown below:
<input type="hidden" name="__VIEWSTATE" value="zSkwjhClKsuaHJSDTmshtSHmomSDbm237NSSJd8sdbj" />
The value part looks like some junk characters. This is encrypted value. The state of all web controls are encrypted and stored here. When you submit the page back to the server, the ASP.NET Framework will retrieve the values of each web controls and assign to them so that you do not need to re populate it.
For example, if you have a DropdownList box in the web page and you populate some values in that from database. When you submit the page to the server by pressing some submit button in the page, the framework will retrieve the values of the Dropdownlist from the ViewState and re populate the drop down automatically. You do not need to re populate it from the database. This works for all web controls.
In old ASP days, you had to re populate all the controls every time you submit to the server.
Can I disable View State?
Yes, you can disable the ViewState of any control by setting the property EnableViewState = False for the control.
You can disable the ViewState for all the controls in a page by setting the property EnableViewState = False for the Form.
Why should I disable ViewState? Is it reccommended to disable ViewState?
It depends on your requirement. ViewState comes with a penalty. It adds the state of all the webcontrols to a hidden field in the page, increasing the size of the page. This may have an impact on slow connections.
You should disable ViewState for the Form for all the pages which do not have any user input controls (All read only pages).
For all the pages which have user input controls (like Textbox, DropdownList etc), you should take an appropriate decision. If you need to retain the values in the controls during postback, you must enable ViewState for such controls. Disable ViewState selectively for each control which do not need to retain the values during postback.
Note that ViewState is enable by default. You should disable it, if you do not need it.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|