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
Problem-1:
1.Counter.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Counter.aspx.vb" Inherits="Counter" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Counter Using ViewState</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCounter" runat="server" Font-Size="Large" Text="Counter: 0"></asp:Label>
<br /><br />
<asp:Button ID="btnIncrement" runat="server" Text="Increment by 2" OnClick="btnIncrement_Click" />
</div>
</form>
</body>
</html>
2.Counter.vb
Partial Class Counter
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("counter") = 0
End If
lblCounter.Text = "Counter: " & ViewState("counter").ToString()
End Sub
Protected Sub btnIncrement_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim count As Integer = CInt(ViewState("counter"))
count += 2
ViewState("counter") = count
lblCounter.Text = "Counter: " & count.ToString()
End Sub
End Class
Problem-2:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Counter.aspx.vb" Inherits="Counter" ViewStateEncryptionMode="Always" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Counter Using ViewState (Encrypted)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCounter" runat="server" Font-Size="Large" Text="Counter: 0"></asp:Label>
<br /><br />
<asp:Button ID="btnIncrement" runat="server" Text="Increment by 2" OnClick="btnIncrement_Click" />
</div>
</form>
</body>
</html>
Problem-3:
3. Customer.vb
<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
4. Default.aspx(cutomer retrievel)
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<title>Customer ViewState Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnStoreCustomers" runat="server" Text="Store Customers" OnClick="btnStoreCustomers_Click" />
<asp:Button ID="btnRetrieveCustomers" runat="server" Text="Retrieve Customers" OnClick="btnRetrieveCustomers_Click" />
<br /><br />
<asp:Literal ID="litOutput" runat="server" />
</form>
</body>
</html>
5. Default.vb(cutomer retrievel)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnStoreCustomers_Click(sender As Object, e As EventArgs)
' Create customer instances
Dim cust1 As New Customer("Areeba", "Khan")
Dim cust2 As New Customer("Bilal", "Nadeem")
Dim cust3 As New Customer("Farah", "Iqbal")
' Add to list
Dim customers As New List(Of Customer)
customers.Add(cust1)
customers.Add(cust2)
customers.Add(cust3)
' Store in ViewState
ViewState("CustomerList") = customers
litOutput.Text = "<b>3 Customers stored successfully in ViewState.</b>"
End Sub
Protected Sub btnRetrieveCustomers_Click(sender As Object, e As EventArgs)
' Retrieve from ViewState
Dim customers As List(Of Customer) = TryCast(ViewState("CustomerList"), List(Of Customer))
If customers IsNot Nothing Then
Dim result As String = "<b>Retrieved Customers:</b><ul>"
For Each cust As Customer In customers
result &= "<li>" & Server.HtmlEncode(cust.FirstName & " " & cust.LastName) & "</li>"
Next
result &= "</ul>"
litOutput.Text = result
Else
litOutput.Text = "<b>No customer data found in ViewState.</b>"
End If
End Sub
End Class