Store your Object in Session (C#) : Session Variables « Session Cookie « ASP.Net

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
ASP.Net » Session Cookie » Session Variables 
Store your Object in Session (C#)

<%@ Page language="c#" src="SessionStateExample.aspx.cs" AutoEventWireup="false" Inherits="SessionStateExample" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:Label id="lblSession" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 32px" runat="server" Width="232px" Height="61px" Font-Size="Medium" Font-Names="Verdana" Font-Bold="True"></asp:Label>
      <DIV style="BORDER-RIGHT: 2px groove; BORDER-TOP: 2px groove; Z-INDEX: 105; LEFT: 16px; BORDER-LEFT: 2px groove; WIDTH: 576px; BORDER-BOTTOM: 2px groove; POSITION: absolute; TOP: 248px; HEIGHT: 160px; BACKGROUND-COLOR: lightyellow" ms_positioning="GridLayout">
        <asp:ListBox id="lstItems" style="Z-INDEX: 106; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" Width="208px" Height="128px"></asp:ListBox>
        <asp:Button id="cmdMoreInfo" style="Z-INDEX: 106; LEFT: 248px; POSITION: absolute; TOP: 24px" runat="server" Width="120px" Height="24px" Text="More Information"></asp:Button>
        <asp:Label id="lblRecord" style="Z-INDEX: 106; LEFT: 248px; POSITION: absolute; TOP: 64px" runat="server" Width="272px" Height="61px" Font-Size="X-Small" Font-Names="Verdana" Font-Bold="True"></asp:Label></DIV>
    </form>
  </body>
</HTML>


<%--
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 += "<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();

    }

    #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);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    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 += "<br>Manufacturer: ";
        lblRecord.Text +=  piece.Description;
        lblRecord.Text += "<br>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;
    }
  }




--%>
           
       
Related examples in the same category
1. Use session to store action time (VB.net)
2. Get Cookie value and assign to the label in C#
3. Get Cookie value (C#)
4. Session Set message (C#)
5. Session get Message (C#)
6. Use Session item (VB.net)
7. Get all session keys (VB.net)
8. Remove value from session value (VB.net)
9. Remove all session values (VB.net)
10. Remove session value by index (VB.net)
11. Write value to session (VB.net)
12. Add and get value from session (VB.net)
13. Clear session values (VB.net)
14. Copy session values (VB.net)
15. Remove all session content (VB.net)
16. Get session code page in VB (VB.net)
17. Session item count (VB.net)
18. Simple session data: set and retrieve
19. Pass variables with & sign (VB.net)
20. Pass variables between pages (VB.net)
21. Use session variables (VB.net)
ww_w___.__j_a_v___a___2___s.__c_o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.