What is View State?
View State is a client side state management mechanism. It can store the page value at the time of post back (i.e Sending and Receiving information from Server) of the page.
In classic ASP when a form is submitted all form values get cleared, So if we have to resubmit the page we have to input all the values again.
In Asp.Net when a form is submitted the form reappears in the browser window together with all form values because ASP .NET maintains ViewState.
ASP.NET pages provide the View State property as a built-in structure for automatically storing values between multiple requests for the same page.
For Example:
Adding one variable in View State:
ViewState["Test"]=Count;
Retrieving information from View State:
string Temp=ViewState["Test"];
View State only contains information about page and its controls. It does not have any interaction with the Server. It stays along with the page in the Client Browser. View State use Hidden field to store its information in a encoding format.
Trace the View State Information:
We can also trace the View State Information, by enabling "Trace" option of Page Directive.
Example:
<%@ Page Language="C#" AutoEventWireup="true" Trace="true"
Now if we run the web application, We can view the details of View State Size along with control ID.
Enabling and Disabling View State:
We can also enable and disable the View state for a single control or for the whole page.
To turnoff view state for a single control,set EnableViewState Property of that control to false.
Example:
TextBox1.EnableViewState =false;
But to turnoff the view state of entire page, we need to set EnableViewState to false in the Page Directive.
Example:
<%@ Page Language="C#" EnableViewState="false"
Advantages:
-
- It is easy to implement.
-
- It does not require any server
resource.
Dis-Advantages:
-
- We cannot store a large ammount of
data or else we will have
performance issue.
-
- It does not support mobile devices.
Controls that do not have their View State maintained or (Controls that do not depend on ViewState):
There are three types of Controls:
1. The standard HTML elements
such as:
<input type="radio" checked="checked">
2. HTML server controls which have the
runat=server tag added such as:
<input type="radio" checked="checked" runat="server">
3. Full web controls such as:
<asp:textbox id="txtMyText" runat="server"></asp:textbox>
Among all the three controls only the Full web controls have their view state maintained, i.e. The first two controls (The standard HTML elements and HTML server controls) do not have their View State maintained or we can say that they do not depend on View State.
0 Comment(s)