How to Manage ViewState in ASP.NET?

We can manage a state with the following methods.

  1. ViewState
  2. Session
  3. Application
  4. Cookie

ViewState

ViewState is used to retain the value of the current page.

Life of ViewState is a life of the current page. Switching to the other page leads to the last page viewstate value expiring. Its value gets changed from one execution of a page to another.

ViewState is enabled or disabled on the page, and control is specific also. It's very flexible to ON / OFF. You can check viewstate on the executed ASP.NET page by viewing the source from the browser. There, you can find the _VIEWSTATE hidden field.

ViewState increases the size of the page because the page and page control values are stored in it. That's why sometimes we avoid using ViewState.

For lightweight value storage, it is very easy and fast to use.

ViewState

Step by Step implementing and using VIEWSTATE on page

Create a new ASP.NET website project.

Website project

Go to File, New, then WebSite, and give the name “ViewStateSample”

Right-click on the project, select ADD, ADD NEW ITEM, and then WebForm from the list of items. I added a web form with the default name. The page name is “DEFAULT.ASPX”

How do you enable and disable ViewState at the page level?

Double-click on Default.aspx page and click the Source option.

Page Level ViewState settings

Open the properties window by pressing the shortcut key CTRL+W+P, Move the cursor at the top of the page, and there this code is written.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

As your cursor reaches the above line, your property window will display the current properties on the ASP.NET page.

Properties

You will get two types of value for selection TRUE and FALSE.

I am going to select TRUE for ON viewstate on my page. As I selected EnableViewState = True, the ASP.NET page value is changed, and this will set like the following:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="True" %>

Control Level ViewState settings: Now, I have inserted a TextBox control on the page. We can also enable or disable ViewState on the control level.

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

Click on the TextBox script of ASP.NET and check Property Window.

ASP.NET

ViewState with the following properties.

  1. Count: To get the number(s) of items in the ViewState.
    Count
  2. Keys: To get all the key names defined for ViewState.
    Keys
  3. Values: To get all the values of the items in the ViewState.
    Values

There are two ways to Add or Update a View State Item.

  1. Syntax
    ViewState.Add("string Key", object value);
    
    • Example
      ViewState.Add("UserAddress", txtAddress.Text);
      
  2. Syntax
    ViewState["string Key"] = object value;
    
    • Example
      ViewState["UserAddress"] = txtAddress.Text;
      

Retrieves the Value from ViewState

txtAddress.Text = (string)ViewState["UserAddress"];
txtAddress.Text = ViewState["UserAddress"].ToString();
txtAddress.Text = Convert.ToString(ViewState["UserAddress"]);

Removes an item from ViewState

ViewState.Remove(“UserAddress”)

Clear all items from ViewState

ViewState.Clear

Clear


Similar Articles