ViewState is the method that is used in ASP.NET to preserve page and control value or we can say it is technique to persist the data of page or control value between round trips. It is one of the Client side state management technique. That is used in Asp.Net.

 As we know when a html page is rendered on browser after round trips then it lost their control value. But we can persist the page and control value during round trips using View State in

In Asp.Net page, ViewState is automatically used to persist the information which can be preserved between post backs. So, ViewState is used to maintain the state of controls during page postbacks.

Why View State

As you know web page or web application is stateless. When we request for a page on browser then it creates a new instance for the page. So, it means every time page information or control value would be lost during round trips. If you fill a form and send it to server for processing but after round trips [when page return from server] then every data would be lost. To overcome this, ViewState technique has introduced.


Note: To use the ViewState property, the ASP.NET Web page must have a form element that has the attribute runat="server".

How to store value in View State

View State uses HiddenField to store page information or control value in key-value format and value stored in base64-encoded strings encrypted format.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTA4OTA3MDE2Nw8WAh4KVmlzaXRDb3VudAIBFgICAw9kFgICBQ8PFgIeBFRleHQFFFBhZ2"?>

To store data in View State, we use View State property.


Enable/Disable ViewState

View State can enable or disable the View State on control level, page level. By default View State is enabled for page.  You can enable or disable ViewSate on page level in page directive. You can change EnableViewState value to true or false.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Employee.aspx.cs" EnableViewState="true" Inherits="WebApplication1.Employee"  %>
Data Types can store in ViewState





Hash Table


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="WebApplication1.Employee" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:TextBox runat="server" ID="txtEmployeeName" />
        <asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
        <br />
        Emplyee Name : <asp:Label runat="server" ID="lblEmployeeName" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
    public partial class Employee : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
               if (ViewState["EmployeeName"] != null)
                    lblEmployeeName.Text = ViewState["EmployeeName"].ToString();
                    lblEmployeeName.Text = "Textbox was empty";

        protected void btnSubmit_Click(object sender, EventArgs e)
            if (txtEmployeeName.Text != "")
                ViewState["EmployeeName"] = txtEmployeeName.Text;
            lblEmployeeName.Text = txtEmployeeName.Text;


Today We learned What is ViewState in and how can we use it.

I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks.