ViewState - DotNet and DataBase

Thursday, 9 August 2018

ViewState



View State is the method to preserve the Value of the Page and Controls across the postback events. Web pages are stateless. So, they can't preserve the values across postbacks. When we need a page variable to be maintained its value between the page postbacks, we should follow the ViewState technique. Between the postback events the ViewState values stores in a hidden variable as below

Right click on page --> Select View source

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="1Gqipsc2k5Jp84oX/9u+9PE0ZqztuNwlvOew4iB4IyRklhL4uItblhttxvSGA43X1GO59yscIiQcmPQualIcYmLckyTxUOikFo4cPy9tuZM=" />


This single hidden field maintains all the ViewState values for all the page controls. 
We can enable and disable the ViewState in control level and page level as below.




Example:

aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="WebProgramming.Welcome" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            &nbsp;<table>
            <tr>
                <td>
                    <asp:Label ID="lblOne" runat="server" Text="Increment" Font-Size="XX-Large"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" Height="42px" Width="298px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblTwo" runat="server" Text="Copy from Increment" Font-Size="XX-Large"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" Height="49px" Width="302px"></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td style="text-align:right">
                   <asp:Button ID="Button1" runat="server" Height="43px" OnClick="Button1_Click" Text="Submit" Width="90px" />
                    </td>
                     <td style="text-align:right">
                   <asp:Button ID="Button2" runat="server" Height="43px" OnClick="Button2_Click" Text="Submit2" Width="90px" />
                    </td>
            </tr>
        </table>

        </div>
       
    </form>
</body>

</html>


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

namespace WebProgramming
{

    public partial class Welcome : System.Web.UI.Page
    {
        int a = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                a = 0;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ViewState["aValue"] != null)
            {
                a = Convert.ToInt32(ViewState["aValue"]);
            }
            ViewState["aValue"] = a + 1;
            TextBox1.Text = (a + 1).ToString();
            Session["aValue"] = ViewState["aValue"];
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text = Convert.ToString(ViewState["aValue"]);
        }
    }
}


ViewState["aValue"] stores the value across the page.


Features Of View State
  1. Retains the value of the Control after post-back without using a session.
  2. Stores the value of Pages and Control Properties defined in the page.
  3. Creates a custom View State Provider that lets you store View State Information in a SQL Server Database or in another data store.

After clicking on the Submit Button the value of increment text box is submitted in View State and the View State stores the value of the increment text box during post-back.
After click on the submit2 Button, we can get the value from ViewState["aValue"]. The Value must be retained during postback and the values are stored into a base 64 encoded string and this information is then put into the View State Hidden Field.

The data Objects That Can be stored in the ViewState
  1. String
  2. Boolean Value
  3. Array Object
  4. Array List Object
  5. Hash Table 
Advantages of View State
  1. Easy to Implement.
  2. No server resources are required: 
Disadvantages of View State
  1. Security Risk: The Information of View State can be seen in the page output source directly. You can manually encrypt and decrypt the contents of a Hidden Field, but It requires extra coding. If security is a concern then consider using a Server-Based state Mechanism so that no sensitive information is sent to the client.
  2. Performance: Performance is not good if we use a large amount of data because View State is stored in the page itself and storing a large value can cause the page to be slow.
  3. Device limitation: Mobile Devices might not have the memory capacity to store a large amount of View State data.
  4. It can store values for the same page only.
View State suitable
  1. When the data to be stored is small.
  2. Try to avoid secure data.

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.