The View State is client state mechanism in the ASP.Net statement management.
The view state is control it will be used to maintain the state of the control across the posted back to the server. The value will be stored in the hidden control. Every time it needs to encryption and decryption (serialize and deserialize across the post backs).
The view state can be enabled by setting the property called EnableViewState="true/false". It can be set in the machine config/ web config/ page directives/ control level.
By default it is false. You can enable it by setting boolean flag.
This can be used with in the page and not across the page.
Viewstate["Version"] = txtVersionName.Text.ToString();
The view state also can store the data using the key and value combination. The data will be serialized and stored in the hidden control You can view the source code page and find the hidden control with _viewstate.
- Increase the page payload (when you have grid with many rows then every thing has to be loaded and retain again with serialize and deserilize)
- Additional overhead while serialize and deserialize.
- Increase the memory allocation on the server.
protected void Button1_Click(object sender, System.EventArgs e)
ViewState["FavoriteColor"] = TextBox1.Text.ToString();
ViewState["City"] = TextBox2.Text.ToString();
Label1.Text = "Your data saved in ViewState.";
protected void Button2_Click(object sender, System.EventArgs e)
string color = (string)ViewState["FavoriteColor"];
string city = (string)ViewState["City"];
Label1.Text = "Hi your favorite color is: " + color +
"<br />and you came from: " + city;
} Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator
Viewstate object is used to persist data of variables across postbacks. In ASP.NET, a variable's value is assigned to a viewstate object and then this is passed as a hidden variable and then may be retrieved by a page after a postback. See the example below...
//Save the value in ViewState object before the PostBack
ViewState["SomeVar"] = txtFirstName.text;
//Retrieve the value from ViewState object after the PostBack
String strFirstName = ViewState["SomeVar"].ToString();
Note that Viewstate object's value is accessible only at page level. This means that if a viewstate is created at page1.aspx, then it may be used only within page1.aspx after the postback, and cannot be used by any other page.
Refer this url
.Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator
Viewstate is used to store data that are accessed by the same page. Ideally a POSTBACK situation.
For example, think of a report. The filter condition is appended as and when the user selects a condition and finally when the user click submit button, the report is generated. Here we can use viewstate to save the filter condition since the same page is posted back. When we use session here, the filter condition is lost on session expiry.
In general, its a good practice to disable viewstate for datagrids for lengthy reports. Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator