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?
Code Implementation
Problem : Counter
Counter.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Counter.aspx.vb" Inherits="Counter" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>ViewState Counter in VB</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center; margin-top:100px;">
<asp:Label ID="lblCounter" runat="server" Font-Size="XX-Large" Text="0"></asp:Label>
<br /><br />
<asp:Button ID="btnIncrement" runat="server" Text="Increment" OnClick="btnIncrement_Click" />
</div>
</form>
</body>
</html>
Counter.aspx.vb
<script runat="server">
Partial Class Counter
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Initialize the counter on first load
If Not IsPostBack Then
ViewState("Counter") = 0
End If
End Sub
Protected Sub btnIncrement_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim counter As Integer = Convert.ToInt32(ViewState("Counter"))
counter += 2
ViewState("Counter") = counter
lblCounter.Text = counter.ToString()
End Sub
End Class
</script>