In this post we will discuss about view state in Asp.Net.
- View state is one of the popular and simple state management technique.
- View state uses a hidden field that ASP.NET automatically inserts in the final, rendered HTML of a web page.
- It is very much helpful to store information that is used for multiple postbacks in a single web page.
- We can enable and disable view state at control level as well as page level.
You can use the EnableViewState= "True/False"; to enable or disable viewstate for control level or page level.
- Example to store value:
ViewState["UserName"] = "AspDotNetHelp";
To retrive the value we can write like this:
string currentUserName = ViewState["UserName"].ToString();
But the view state data is not secure, because Asp.Net uses a Base64 string.
- But if your view state data contains secret data then you can encrypt that data by using
ViewStateEncryptionMode property at page level or application level like below:
<%@Page ViewStateEncryptionMode="Always" %>
<pages viewStateEncryptionMode="Always" />