Tuesday, May 20, 2014

What is ViewState ?

Web Applications work on HTTP protocol. HTTP protocol is a stateless protocol, meaning it does not retain state between user requests.

Here an example to understand the stateless protocol :-
WebForm1.aspx  :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FirstApplication.WebForm1" %>  
 <!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 ID="TextBox1" runat="server"></asp:TextBox>  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  
WebForm1.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     int clickcount = 0;  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if(!IsPostBack){  
         TextBox1.Text = "0";  //If it is an initial get request.
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       clickcount = clickcount + 1;  
       TextBox1.Text = clickcount.ToString();  
     }  
   }  
 }  

Web forms live for barely a moment. When a request is received
1. An instance of the requested webform is created.
2. Events Processed.
3. Generates the HTML & posted to the client.
4. The web form is immediately destroyed.

               In the above example every time you click the button new instance of the web form will be created. For the first time it will go into “if condition statements” because it is an initial get request. After the first click it won’t go. For the second click clickcount will be 1. Beyond the second click the clickcount value will not change stays at 1 because every time you click the button new instance of the web form will be created. Every time you post this web form web server doesn’t remember the state of the between request. To make the server to remember the state in ASP.NET has the ViewState field.
Using ViewState field Correction of above code  :-
WebForm2.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm2 : System.Web.UI.Page  
   {  
     int clickcount = 1;  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if(!IsPostBack){  
         TextBox1.Text = "0";  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       if(ViewState["Clicks"]!=null){  
         clickcount = (int)ViewState["Clicks"] + 1;  
       }  
       TextBox1.Text = clickcount.ToString();  
       ViewState["Clicks"] = clickcount;  
     }  
   }  
 }  

        Here “Clicks” is a ViewState variable to store the clickcounts. The ViewState data travels with every request and response between the client and the web server in hidden field data formate.

Another way to solve this problem  :-
WebForm3.aspx.cs  :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace FirstApplication  
 {  
   public partial class WebForm3 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         TextBox1.Text = "0";  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       int clickcount = Convert.ToInt32(TextBox1.Text) + 1;  
       TextBox1.Text = clickcount.ToString();  
     }  
   }  
 }  

No comments:

Post a Comment