Store user defined object in Session (C#) : Session Variables « Sessions « ASP.NET Tutorial






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;
  }
}








11.3.Session Variables
11.3.1.Using Session State
11.3.2.Retrieve the value of an item that you have stored in Session state.
11.3.3.Sorting a DataView stored in Session state.
11.3.4.Save value in text box to session and read it back (VB.net/C#)
11.3.5.Save value to session object and read them back (VB.net)
11.3.6.Store user defined object in Session (C#)
11.3.7.Setting and retrieving objects from the Session using State Service and a base page (C#)
11.3.8.Setting and retrieving objects from the Session using State Service and a base page (VB)
11.3.9.A session-aware base page
11.3.10.Session counter and application counter (C#)
11.3.11.Session counter and application counter (VB)
11.3.12.Look for Variables