Session requests are related to the customer. Each customer has its own Session. For example, shopping cart. Initialize the Session. Initialize the content when each session is added. Code Public class Global: System. Web. HttpApplication
{
//...
Protected void Session_Start (object sender, EventArgs e)
{
// Initialization, shopping cart session
Session ["Cart"] = new ArrayList ();
Logger. Log (string. Format ("Session addition: {0}", Context. Session. SessionID ));
}
}
Purchase pencil page:
Code Public partial class BuyPencileForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
ArrayList arry = (ArrayList) Session ["Cart"];
Arry. Add (new Cart ("Pencile", 10 ));
}
}
Pen purchase page:
Code Public partial class BuyRedPenForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void Button2_Click (object sender, EventArgs e)
{
// Settlement:
Response. Redirect ("TotalForm. aspx ");
}
Protected void button#click (object sender, EventArgs e)
{
ArrayList arry = (ArrayList) Session ["Cart"];
Arry. Add (new Cart ("RedPeng", 30 ));
}
}
Settlement page:
Code
Public partial class TotalForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Int totalCount = 0;
ArrayList list = (ArrayList) Session ["Cart"];
Foreach (Cart item in list)
{
TotalCount + = item. Cost;
Response. Output. WriteLine (string. Format ("goods name: {0}, goods price: {1}", item. Description, item. Cost. ToString ()));
Response. Output. WriteLine ("<br/> ");
}
Response. Output. WriteLine (string. Format ("Total: {0}", totalCount. ToString ()));
}
}
Add a variable to the session when purchasing the page, and read the Session on the settlement page.