What is a view state?
View state in simple terms can be defined as a way to maintain the state of an aspx page during postbacks. And this is the no 1 one liner answer I get from candidates during interviews. Viewstate is a technique which helps in retaining the values of the controls placed in a page during postback/roundtrip. With this blog, again, my aim is the same, to spread more light on what is view state all about.
What do you mean by state of an aspx page?
The state of an aspx page means the values of the controls in an aspx page. By default the view state is maintained for all the controls which are placed in an aspx page. The state of a control can be the text entered in a text box, the checked status of a radio button or check box, the content and selected item in a drop down control etc.
Where is view state stored?
View state is stored in the client in the form of a hidden control called “__VIEWSTATE”. Here by client I mean the rendered aspx pages in the browser. If view state property is set to true then ASP.NET automatically adds a hidden field/control by the name “__VIEWSTATE” and stores the information related to the state of the page in an encrypted format.
What is the encoding used in view state?
View state is stored in base64 format. The base64 format can generate huge amount of data on conversion and this is the reason we are adviced to use view state judiciously.
Which class handles view state?
StateBag class handles the view state and it implements a Dictionary to keep track of the changes in a form. The developer can add or remove items from this dictionary as you would from any dictionary object. You can access this class through the Control.ViewState property.
Can view state be stored in multiple hidden fields?
Yes, view state can be stored in multiple hidden fields. This can be achieved by setting the page’ “MaxPageStateFieldLength” property. If the property is set to a positive value then the view state is broken and sent to the client in multiple hidden field and each field’ size will be less than the value spedified in “MaxPageStateFieldLength”.
How to disable view state?
View state can be disabled in the following ways.
1. In machine.config – you can disable view state for all the application hosted in the server by modifying the following tag in machine.config as shown below.
<pages enableviewstatemac="true" enableviewstate="false"> |
<system.web> <pages enableviewstate="false"> </system.web> |
3. Page level – you can use this option to switch off the view state for all the controls in a web page by setting the “EnableViewState” page attribute to false as shown below.
<%@ Page EnableViewState="false" ... %> |
4. Control level - you can switch off a control’ view state by setting the EnableViewState property of the control to false from property window or as shown below.
<asp:label id="”Label1”" enableviewstate="false" runat="”server"> |
Hope the above details give a clearer picture on what view state is. There is more to view state than what is written here.
the info given by u is vry simple and easy to undrstnd...
ReplyDeletethnQ..........
tha info is really very simple but if u provide me d correct defntn of view state & session den it will b better for me . . plz. .
ReplyDeletethanku
This article provides a comprehensive overview of ASP.NET View State, highlighting its importance in maintaining state across postbacks—essential for optimizing web applications! If you're also a gamer, q Flipper is a great tool to help you manage your game library while you fine-tune your ASP.NET projects.
ReplyDelete