IAD LAB-6: VIEWSTATE

Problem Statement:

Problem 1

Implement a simple program that uses viewstate to store and retrieve counter value. Whenever Increment button is clicked counter should be incremented by 2. Write code behind below.

If this page is accessed using different browser windows, or from more than one clients, whether the counter value would be same, or it would be different at different time instants? Give reason for the observation.

Problem 2

Extend the program implemented above and make the View State secure. Refer to the textbook for the technique of securing.

Problem 3

Consider the following definition of customer class. Develop a web form that stores three objects of customer in viewstate. Implement the code for retrieving customer objects also.

Code Implementation


Unencrypted Viewstate

Partial Class Counter Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load ' Set default counter to 0 if first time loading If Not IsPostBack Then ViewState("Counter") = 0 lblCounter.Text = "Counter: 0" End If End Sub Protected Sub btnIncrement_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnIncrement.Click Dim count As Integer = CType(ViewState("Counter"), Integer) count += 2 ViewState("Counter") = count lblCounter.Text = "Counter: " & count.ToString() End Sub End Class
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Counter" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple Counter</title> </head> <body> <form id="form1" runat="server"> <div> <h2>TASK 1: Viewstate Without Encryption</h2> <h2>Click to increase the counter by 2</h2> <asp:Label ID="lblCounter" runat="server" Text="Counter: 0" Font-Size="Large"></asp:Label><br /><br /> <asp:Button ID="btnIncrement" runat="server" Text="Increment" /> </div> <br><br><br><br><br><br> <div> <h2>TASK 2: Viewstate With Encryption</h2> <a href="EncryptedViewstate.aspx">Visit Encrypted Viewstate</a><br /> <h2>TASK 3: Serialization Class</h2> <a href="seiralization.aspx">Visit Class Serialization</a> </div> </form> </body> </html>

Encrypted Viewstate

Partial Class Default2 Inherits System.Web.UI.Page Private Sub Default2_Load(sender As Object, e As EventArgs) Handles Me.Load If Not IsPostBack Then ViewState("Counter") = 0 lblCounter.Text = "Counter: 0" End If End Sub Private Sub btnIncrement_Click(sender As Object, e As EventArgs) Handles btnIncrement.Click Dim count As Integer = CType(ViewState("Counter"), Integer) count += 2 ViewState("Counter") = count lblCounter.Text = "Counter: " & count End Sub End Class
<%@ Page Language="VB" ViewStateEncryptionMode="Always" AutoEventWireup="false" CodeFile="EncryptedViewstate.aspx.vb" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2>Encrypted Viwestate Counter Value</h2> <asp:Label ID="lblCounter" runat="server" Text="Counter: 0"></asp:Label> <br /> <br /> <asp:Button ID="btnIncrement" runat="server" Text="Increment" /> <br /> <br /> <br /> </div> </form> </body> </html>

Class Serialization

Partial Class Default2 Inherits System.Web.UI.Page Protected Sub btnSubmitCustomer_Click(sender As Object, e As EventArgs) Handles btnSubmitCustomer.Click Dim first As String = txtFirstName.Value Dim last As String = txtLastName.Value Dim cust As New Customer(first, last) Dim customers As List(Of Customer) If ViewState("CurrentCustomer") IsNot Nothing Then customers = CType(ViewState("CurrentCustomer"), List(Of Customer)) Else customers = New List(Of Customer) End If customers.Add(cust) ViewState("CurrentCustomer") = customers lblsubmitted.Text = "The Customer " & first & " " & last & " has been Submitted Successfully!" End Sub Protected Sub btnRetrieveCustomer_Click(sender As Object, e As EventArgs) Handles btnRetrieveCustomer.Click If ViewState IsNot Nothing Then lblCustomers.Text = "" Dim customers As List(Of Customer) = CType(ViewState("CurrentCustomer"), List(Of Customer)) For Each cust As Customer In customers lblCustomers.Text &= cust.FirstName & " " & cust.LastName & "<br />" Next End If End Sub End Class <Serializable()> Public Class Customer Private _firstName As String Public Property FirstName() As String Get Return _firstName End Get Set(ByVal Value As String) _firstName = Value End Set End Property Private _lastName As String Public Property LastName() As String Get Return _lastName End Get Set(ByVal Value As String) _lastName = Value End Set End Property Public Sub New(ByVal firstName As String, ByVal lastName As String) Me.FirstName = firstName Me.LastName = lastName End Sub End Class
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="serialization.aspx.vb" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <br /> <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label> <br /> <input id="txtFirstName" type="text" runat="server" /><br /> <br /> <asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label> <br /><input id="txtLastName" type="text" runat="server"/><br /> <br /> <asp:Button ID="btnSubmitCustomer" runat="server" Text="Submit Customer" Width="160px" /> <br /> <asp:Label ID="lblsubmitted" runat="server"></asp:Label> <br /> <br /> <asp:Button ID="btnRetrieveCustomer" runat="server" Text="Retrieve Customer" /> <br /> <asp:Label ID="lblCustomers" runat="server"></asp:Label> <br /> <br /> </div> </form> </body> </html>

Sample Output


TASK 1

The counter value will be different for each browser window or client because ViewState is stored on the client side and is specific to a single page instance. When a user interacts with the page, the ViewState is sent back and forth between the server and that particular client only, making it unique to that session and page. Therefore, multiple users or different browser instances will have their own independent ViewState data, resulting in different counter values at different times.


TASK 2

View state encryption can be enabled either for an individual page using the ViewStateEncryptionMode attribute in the Page directive, or globally for all pages through the configuration file by setting <pages viewStateEncryptionMode="Always" /> inside <system.web>. This ensures that the view state is always encrypted. The available settings are Always, Never, and Auto. By default, it is set to Auto, which means view state is only encrypted if a specific control on the page requires it.


TASK 3

In ASP.NET, custom objects can be stored in view state, but they must be serializable, meaning they need to be converted into bytes. If the object is not serializable, an error will occur. To make it serializable, the [Serializable] attribute must be added to the class.

Live Demonstration: Live Web Page Demosntration

Web hosting by Somee.com