Creating Session
copy the following code in file with extension .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="session_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label_Session" runat="server" Width="472px" Font-Size="Medium"
Font-Names="Verdana" Font-Bold="True"></asp:Label>
<br /><br />
<div style="border-right: 2px groove; border-top: 2px groove; border-left: 2px groove; width: 576px; border-bottom: 2px groove; HEIGHT: 160px; background-color: #FFFFD9">
<table>
<tr>
<td style="padding: 10px">
<asp:ListBox id="ListBox_items" Width="208px" Height="128px" runat="server"></asp:ListBox>
</td>
<td style="padding: 10px">
<asp:Button id="Button_MoreInfo" Width="120px" Text="More Information" runat="server"
OnClick="cmdMoreInfo_Click"></asp:Button>
<br />
<br />
<asp:Label id="Label_result" runat="server" Width="272px" Font-Size="Small"
Font-Names="Verdana" Font-Bold="True"></asp:Label>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
copy the following code in file with extension .aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class session_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//creating objects of Furniture class
Furniture furniture1 = new Furniture("Econo Sofa","Acme Inc.", 60.99M);
Furniture furniture2 = new Furniture("Pioneer Table","Heritage Unit", 800.75M);
Furniture furniture3 = new Furniture("Retro Cabinet","Sixties Ltd.", 250.11M);
// Add objects to session state.
Session["Furniture1"] = furniture1;
Session["Furniture2"] = furniture2;
Session["Furniture3"] = furniture3;
// Add rows to list control.
ListBox_items.Items.Add(furniture1.Name);
ListBox_items.Items.Add(furniture2.Name);
ListBox_items.Items.Add(furniture3.Name);
// Display some basic information about the session.
// This is useful for testing configuration settings.
Label_Session.Text = "Session ID: " + Session.SessionID;
Label_Session.Text += "<br />Number of Objects: ";
Label_Session.Text += Session.Count.ToString();
Label_Session.Text += "<br />Mode: " + Session.Mode.ToString();
Label_Session.Text += "<br />Is Cookieless: ";
Label_Session.Text += Session.IsCookieless.ToString();
Label_Session.Text += "<br />Is New: ";
Label_Session.Text += Session.IsNewSession.ToString();
Label_Session.Text += "<br />Timeout (minutes): ";
Label_Session.Text += Session.Timeout.ToString();
}
}
protected void cmdMoreInfo_Click(object sender, EventArgs e)
{
if (ListBox_items.SelectedIndex == -1)
{
Label_result.Text = "No item is selected";
}
else
{
// Construct string key name based on the index to retrive session value
string key = "Furniture" +
(ListBox_items.SelectedIndex + 1).ToString();
// Retrieve the Furniture object from session state.
Furniture piece = (Furniture)Session[key];
// Display the information for this object.
Label_result.Text = "Name: " + piece.Name;
Label_result.Text += "<br />Manufacturer: ";
Label_result.Text += piece.Description;
//"c" is used to display $ sign before cost
Label_result.Text += "<br />Cost: " + piece.Cost.ToString("c");
}
}
}
create a class file with name Furniture.cs and copy the following code in the file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Furniture
/// </summary>
public class Furniture
{
public string Name;
public string Description;
public decimal Cost;
public Furniture(string name, string description,
decimal cost)
{
Name = name;
Description = description;
Cost = cost;
}
}
No comments:
Post a Comment