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 asp.net.

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.

ViewState[“NameOfKey”]=”YourValue”;

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

Integers

String

Boolean

Array/ArrayList

Hash Table

Example

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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" />
    </div>
    </form>
</body>
</html>
Employee.aspx.cs
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();
                else
                    lblEmployeeName.Text = "Textbox was empty";
            
        }

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

        }
    }
}
Conclusion:

Today We learned What is ViewState in asp.net 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.