It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.
ViewState does not hold the controls, it holds the values of controls. It does not restore the value to control after page post back.
Step 1: Now create the project as:
“Start” –> “All Programs”- “Microsoft Visual Studio 2012”.
“File” – “New Project” – “C#” – “Empty Project” (to avoid adding a master page).
Give the Project name such as ViewStateExample or another as you wish and specify the location.
Then right-click on Solution Explorer – “Add New Item” – “WebForm1.aspx” page.
Step 2:Open WebForm1.aspx page and write this code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="VewStateExample.WebForm1" %>
var msg = ‘<%=ViewState["Msg"].ToString()%>’;
<form id="form1" runat="server">
Step 3: Open “WebForm.aspx.cs” page and write this code on page load.
public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//You can see Inside the Page Load event, the ViewState object is set with a String value.
ViewState["Msg"] = "Welcome To LanguageTechFunda";