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 "DangerousPets". 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.cs
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// nothing added
}
protected void btnSubmitOrder_Click(object sender, EventArgs e)
{
// alligators
if (!String.IsNullOrEmpty(txtAlligators.Text))
System.Web.HttpContext.Current.Session["Alligators"] =
txtAlligators.Text;
// Banana Slugs
if (!String.IsNullOrEmpty(txtBananaSlugs.Text))
System.Web.HttpContext.Current.Session["BananaSlugs"] =
txtBananaSlugs.Text;
// Black Widows
if (!String.IsNullOrEmpty(txtBlackWidows.Text))
System.Web.HttpContext.Current.Session["BlackWidows"] =
txtBlackWidows.Text;
// Fugu
if (!String.IsNullOrEmpty(txtFugu.Text))
System.Web.HttpContext.Current.Session["Fugu"] = txtFugu.Text;
// Rattlesnakes
if (!String.IsNullOrEmpty(txtRattlesnakes.Text))
System.Web.HttpContext.Current.Session["Rattlesnakes"] =
txtRattlesnakes.Text;
// Scorpions
if (!String.IsNullOrEmpty(txtScorpions.Text))
System.Web.HttpContext.Current.Session["Scorpions"] =
txtScorpions.Text;
// Venus Fly Traps
if (!String.IsNullOrEmpty(txtVenusFlyTraps.Text))
System.Web.HttpContext.Current.Session["VenusFlyTraps"] =
txtVenusFlyTraps.Text;
Response.Redirect("Order.aspx");
}
}
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.cs
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Order : System.Web.UI.Page
{
// Variable Declarations
// counts for products
protected int alligatorCount;
protected int bananaSlugCount;
protected int blackWidowsCount;
protected int fuguCount;
protected int rattlesnakeCount;
protected int scorpionCount;
protected int venusFlyTrapsCount;
// subtotals for each quantity of
// ordered items
protected double alligatorAmount;
protected double bananaSlugAmount;
protected double blackWidowsAmount;
protected double fuguAmount;
protected double rattlesnakeAmount;
protected double scorpionAmount;
protected double venusFlyTrapsAmount;
// sums for both quantity and amount
protected int totalCount;
protected double totalAmount;
protected void Page_Load(object sender, EventArgs e)
{
// Alligator count and total
try
{
alligatorCount =
Convert.ToInt32(
System.Web.HttpContext.Current.Session["Alligators"]);
}
catch
{
alligatorCount = 0;
}
// Total
alligatorAmount = alligatorCount * 22.15;
// Banana Slug count and total
try
{
bananaSlugCount =
Convert.ToInt32(
System.Web.HttpContext.Current.Session["BananaSlugs"]);
}
catch
{
bananaSlugCount = 0;
}
// Total
bananaSlugAmount = bananaSlugCount * 1.25;
// Black Widows
try
{
blackWidowsCount =
Convert.ToInt32(
System.Web.HttpContext.Current.Session["BlackWidows"]);
}
catch
{
blackWidowsCount = 0;
}
// Total
blackWidowsAmount = blackWidowsCount * 2.99;
// Fugu
try
{
fuguCount =
Convert.ToInt32(
System.Web.HttpContext.Current.Session["Fugu"]);
}
catch
{
fuguCount = 0;
}
// Total
fuguAmount = fuguCount * 9.0;
// Rattlesnake
try
{
rattlesnakeCount =
Convert.ToInt32(
System.Web.HttpContext.Current.Session["Rattlesnakes"]);
}


Join the conversation! Your thoughts help the community grow.