<%@ Page language="c#" src="SessionStateExample.aspx.cs" AutoEventWireup="false" Inherits="SessionStateExample" %>
<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class SessionStateExample : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblSession;
protected System.Web.UI.WebControls.ListBox lstItems;
protected System.Web.UI.WebControls.Button cmdMoreInfo;
protected System.Web.UI.WebControls.Label lblRecord;
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
// Create Product objects.
Product piece1 = new Product("Econo Sofa",
"Acme Inc.", 7.9M);
Product piece2 = new Product("Pioneer Table",
"Heritage Unit", 8.7M);
Product piece3 = new Product("Retro Cabinet",
"Sixties Ltd.", 3.1M);
// Add objects to session state.
Session["Product1"] = piece1;
Session["Product2"] = piece2;
Session["Product3"] = piece3;
// Add rows to list control.
lstItems.Items.Clear();
lstItems.Items.Add(piece1.Name);
lstItems.Items.Add(piece2.Name);
lstItems.Items.Add(piece3.Name);
}
// Display some basic information about the session.
// This is useful for testing configuration settings.
lblSession.Text = "Session ID: " + Session.SessionID;
lblSession.Text += "
Number of Objects: ";
lblSession.Text += Session.Count.ToString();
lblSession.Text += "
Mode: " + Session.Mode.ToString();
lblSession.Text += "
Is Cookieless: ";
lblSession.Text += Session.IsCookieless.ToString();
lblSession.Text += "
Is New: ";
lblSession.Text += Session.IsNewSession.ToString();
lblSession.Text += "
Timeout (minutes): ";
lblSession.Text += Session.Timeout.ToString();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.cmdMoreInfo.Click += new System.EventHandler(this.cmdMoreInfo_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmdMoreInfo_Click(object sender, System.EventArgs e)
{
if (lstItems.SelectedIndex == -1)
{
lblRecord.Text = "No item selected.";
}
else
{
// Construct the right key name based on the index.
string key = "Product" +
(lstItems.SelectedIndex + 1).ToString();
// Retrieve the Product object from session state.
Product piece = (Product)Session[key];
// Display the information for this object.
lblRecord.Text = "Name: " + piece.Name;
lblRecord.Text += "
Manufacturer: ";
lblRecord.Text += piece.Description;
lblRecord.Text += "
Cost: $" + piece.Cost.ToString();
}
}
}
public class Product
{
public string Name;
public string Description;
public decimal Cost;
public Product(string name, string description,
decimal cost)
{
Name = name;
Description = description;
Cost = cost;
}
}
--%>