Introduction
This article describes a simple approach to storing, retrieving, and
redisplaying web pages. Such might be useful if you need to store exact copies
of confirmation pages (e.g., following a sales transaction) or something along
those lines.
The example provided is in the form of a simple application that simulates a
sales oriented web site; the example uses an SQL Server database and LINQ to
SQL. Pages are stored and retrieved from the database for redisplay in a web
browser.

Figure 1: Demo Website
Getting Started:
In order to get started, unzip the included project and save it to your hard
drive. Open the web application and examine the contents in the solution
explorer.

Figure 2: Solution Explorer
The solution contains a single web application project called "WebPageStore".
This web application contains three web pages (default, order, and review) along
with a single master page; all code behind is in VB. The default web page is
used to display a simulation or an ordering system, the order web page is used
to display a simulation of an order confirmation, and the review page is used to
display retrieved web pages.
The app code folder contains a DBML file supporting the LINQ to SQL interface to
the database.
The bulk of the code is there just to provide a simple framework for creating
something to save and restore for redisplay. The relevant parts of the code used
to actually store and recall the order page will be pointed out in the following
sections.
Code: Default.aspx
The default page is used to create an order so that we have something to save to
the database. It is a nonsense web page that badly simulates an online ordering
system for a fake company called, "Dangerous Pets". The purpose of the page is
to allow the user to review product descriptions and enter a count for the
number of items that the user wishes to order at the given price.
This information is used to generate an order confirmation and it is the order
confirmation that is persisted to the database.
There is nothing particularly interesting about the page's markup; it contains
little more than a table used to display a fictitious catalog of scary pets
available for sale. You may open the page from the attached demonstration
project if you'd like to review its content; it won't include it here as it is
not important to the issue of this article.
Code: Default.vb
The code behind for the default page it equally uninspiring; it contains only a
single button click event handler used to complete a phony order by pushing any
order values captured from the default page into session variables and then
redirecting the user to the order page.
Protected Sub
btnSubmitOrder_Click(ByVal sender
As Object, _
ByVal e As
System.EventArgs) Handles btnSubmitOrder.Click
' alligators
If (Not
String.IsNullOrEmpty(txtAlligators.Text))
Then
Session("Alligators") =
txtAlligators.Text
End If
' Banana Slugs
If (Not
String.IsNullOrEmpty(txtBananaSlugs.Text))
Then
Session("BananaSlugs") =
txtBananaSlugs.Text
End If
' Black Widows
If (Not
String.IsNullOrEmpty(txtBlackWidows.Text))
Then
Session("BlackWidows") =
txtBlackWidows.Text
End If
' Fugu
If (Not
String.IsNullOrEmpty(txtFugu.Text))
Then
Session("Fugu") =
txtFugu.Text
End If
' Rattlesnakes
If (Not
String.IsNullOrEmpty(txtRattlesnakes.Text))
Then
Session("Rattlesnakes") =
txtRattlesnakes.Text
End If
' Scorpions
If (Not
String.IsNullOrEmpty(txtScorpions.Text))
Then
Session("Scorpions") =
txtScorpions.Text
End If
' Venus Fly Traps
If (Not
String.IsNullOrEmpty(txtVenusFlyTraps.Text))
Then
Session("VenusFlyTraps") =
txtVenusFlyTraps.Text
End If
Response.Redirect("Order.aspx")
End Sub
End Class
Code: Order.aspx
The order page is used to display a phony order confirmation page. There is
nothing particularly interesting about it; it merely displays the counts for
each item ordered along with a price total. Again, you can review the content of
the page from the attached project.
Code: Order.vb
The form class contains the code used to save the page to an SQL Server
database. This class begins with the declaration of some variables used to
display the order information on the order page:
Partial Class
Order
Inherits System.Web.UI.Page
' Variable Declarations
' counts for products
Protected alligatorCount
As Integer
Protected bananaSlugCount As
Integer
Protected blackWidowsCount As
Integer
Protected fuguCount As
Integer
Protected rattlesnakeCount As
Integer
Protected scorpionCount As
Integer
Protected venusFlyTrapsCount As
Integer
' subtotals for each quantity of
' ordered items
Protected alligatorAmount
As Double
Protected bananaSlugAmount As
Double
Protected blackWidowsAmount As
Double
Protected fuguAmount As
Double
Protected rattlesnakeAmount As
Double
Protected scorpionAmount As
Double
Protected venusFlyTrapsAmount As
Double
' sums for both quantity and amount
Protected totalCount
As Integer
Protected totalAmount As
Double
Following the variable declarations, the page load event handler first populates
all of the variables with the counts, subtotals for each item, and final total
for the order.
''' <summary>
''' On page load, sum up all the quantities,
subtotals
''' and totals for display and place each
''' calculation into the session. If the user
''' buys a product, save the page to an sql server
''' database.
''' </summary>
''' <param
name="sender"></param>
''' <param
name="e"></param>
'''
<remarks></remarks>
Protected
Sub Page_Load(ByVal sender
As Object, _
ByVal e As
System.EventArgs) Handles
Me.Load
' Alligator count and total
Try
alligatorCount = Convert.ToInt32(Session("Alligators"))
Catch ex As
Exception
alligatorCount = 0
End Try
' Total
alligatorAmount = alligatorCount * 22.15
' Banana Slug count and total
Try
bananaSlugCount = Convert.ToInt32(Session("BananaSlugs"))
Catch ex As
Exception
bananaSlugCount = 0
End Try
' Total
bananaSlugAmount = bananaSlugCount * 1.25
' Black Widows
Try
blackWidowsCount = Convert.ToInt32(Session("BlackWidows"))
Catch ex As
Exception
blackWidowsCount = 0
End Try
' Total
blackWidowsAmount = blackWidowsCount * 2.99
' Fugu
Try
fuguCount = Convert.ToInt32(Session("Fugu"))
Catch ex As
Exception
fuguCount = 0
End Try
' Total
fuguAmount = fuguCount * 9.0
' Rattlesnake
Try
rattlesnakeCount = Convert.ToInt32(Session("Rattlesnakes"))
Catch ex As
Exception
rattlesnakeCount = 0
End Try
' Total
rattlesnakeAmount = rattlesnakeCount * 12.77
' Scorpions
Try
scorpionCount = Convert.ToInt32(Session("Scorpions"))
Catch ex As
Exception
scorpionCount = 0
End Try
' Total
scorpionAmount = scorpionCount * 1.55


Join the conversation! Your thoughts help the community grow.