File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SessionStateExample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblSession"
runat="server"
Width="472px"
Font-Size="Medium"
Font-Names="Verdana"
Font-Bold="True"></asp:Label>
<br /><br />
<div >
<table>
<tr>
<td>
<asp:ListBox id="lstItems"
Width="208px"
Height="128px"
runat="server"></asp:ListBox>
</td>
<td style="padding: 10px">
<asp:Button id="cmdMoreInfo"
Width="120px"
Text="More Information"
runat="server"
OnClick="cmdMoreInfo_Click"></asp:Button>
<br />
<br />
<asp:Label id="lblRecord"
runat="server"
Width="272px"
Font-Size="Small"
Font-Names="Verdana"
Font-Bold="True"></asp:Label>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SessionStateExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Product piece1 = new Product("A","A Inc.", 7.9M);
Product piece2 = new Product("B","B Inc.", 6.5M);
Product piece3 = new Product("C","C Ltd.", 3.1M);
Session["Product1"] = piece1;
Session["Product2"] = piece2;
Session["Product3"] = piece3;
lstItems.Items.Add(piece1.Name);
lstItems.Items.Add(piece2.Name);
lstItems.Items.Add(piece3.Name);
}
lblSession.Text = "Session ID: " + Session.SessionID;
lblSession.Text += "<br />Number of Objects: ";
lblSession.Text += Session.Count.ToString();
lblSession.Text += "<br />Mode: " + Session.Mode.ToString();
lblSession.Text += "<br />Is Cookieless: ";
lblSession.Text += Session.IsCookieless.ToString();
lblSession.Text += "<br />Is New: ";
lblSession.Text += Session.IsNewSession.ToString();
lblSession.Text += "<br />Timeout (minutes): ";
lblSession.Text += Session.Timeout.ToString();
}
protected void cmdMoreInfo_Click(object sender, EventArgs e)
{
if (lstItems.SelectedIndex == -1)
{
lblRecord.Text = "No item selected.";
}
else
{
string key = "Product" +(lstItems.SelectedIndex + 1).ToString();
Product piece = (Product)Session[key];
lblRecord.Text = "Name: " + piece.Name;
lblRecord.Text += "<br />Manufacturer: ";
lblRecord.Text += piece.Description;
lblRecord.Text += "<br />Cost: " + piece.Cost.ToString("c");
}
}
}
class Product
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private decimal cost;
public decimal Cost
{
get { return cost; }
set { cost = value; }
}
public Product(string name, string description,
decimal cost)
{
Name = name;
Description = description;
Cost = cost;
}
}