programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Website by URL, How to Play YouTube Videos in by using Jquery , Jquery Change div background on mouseover in , JQuery UI Datepicker (Calendar) etc.

Thursday, July 10, 2014

Use of ViewState in

Use of ViewState in Asp.Net 

ViewState  in 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 Programming.
ViewState["< variable name >"]=< value >;

 in 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


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" "">
<html xmlns="">
<head runat="server">
    <title>Example_viewState </title>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

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

Other Post :

Reference :