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.
Posted Comments :
Daljit Posted : 5 Years Ago
Thanks for sharing this article and also thanks for example.
stored procedure Posted : 5 Years Ago
Good article https://www.aspdotnet.tech/
puja Posted : 3 Years Ago
gy
puja Posted : 3 Years Ago
gy
puja Posted : 3 Years Ago
gy
puja Posted : 3 Years Ago
gy
Geeta Posted : 2 Years Ago
Good
siddhant kumar Posted : 5 Years Ago
Hi, i want to display my data in descending order ..please help me to fix it protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[2] {new DataColumn("Name"), new DataColumn("Country") }); ViewState["Customers"] = dt; this.BindGrid(); } } protected void BindGrid() { GridView1.DataSource = (DataTable)ViewState["Customers"]; GridView1.DataBind(); } protected void Insert(object sender, EventArgs e) { DataTable dt = (DataTable)ViewState["Customers"]; dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim()); ViewState["Customers"] = dt; this.BindGrid(); txtName.Text = string.Empty; txtCountry.Text = string.Empty; } <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false" EmptyDataText="No records has been added."> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" /> </Columns> </asp:GridView> <br /> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse"> <tr> <td style="padding-bottom: 10px"> Name:<br /> <asp:TextBox ID="txtName" runat="server" /> </td> </tr> <tr> <td style="padding-bottom: 10px"> Country:<br /> <asp:TextBox ID="txtCountry" runat="server" /> </td> </tr> <tr> <td style="width: 100px"> <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" /> </td> </tr> </table>