Use of ViewState in Asp.net

Use of ViewState in Asp.Net 

ViewState  in asp.net Programming use to save data as a variable. And the show this data in to the page .this is work as a class variable as we know that. when we create a class variable and save some value in this variable then we can access this variable to any where in the class.

Syntax of View State is :

A view state in  the web programming when we need to take a variable as a class variable.then we need to create  View State["variable name"].And Access this in a same page any where in the page in asp.net Programming.
ViewState["< variable name >"]=< value >;

 in asp.net Programming , user free to give the name of ViewState and give the value.User give any type of value without cousting.but remeber that it cast automatically in object type.

  So when we need to use this variable in next time then we cast it.For Example if we need to store this value in String then we write :


String S=ViewState["Msg"].ToString();


or if we want to so this on the Lable then


Label1.Text=ViewState["Msg"].ToString();



In the web programming the view state use basically Store the Control's Status.

 View state is a mechanism used by ASP.NET Programming to store server controls’ status between page post-backs. The view state information is stored as an HTML hidden variable in forms and sent in the page’s response back to the user. When the user makes the next request, the view state is returned with his or her request. When the page processes, ASP.NET pulls the view state from the page and uses it to reset property values of the page and its controls.

View state example



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

code of .aspx page :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mypage.aspx.cs" Inherits="mypage" %>
<!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>Example_viewState </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>lucknow</asp:ListItem>
            <asp:ListItem>Kanpur</asp:ListItem>
            <asp:ListItem>delhi</asp:ListItem>
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
    </div>  
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>


code for .aspx.cs page :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mypage : System.Web.UI.Page
{    protected void Page_Load(object sender, EventArgs e)
    {        if(!IsPostBack)
             {  // declear view State variable.
                  ViewState["Msg"] = "Hello";          
              }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {   //display view state variable in label
        Label1.Text=ViewState["Msg"].ToString();
    }
}

Other Post :




Reference :