Asp.net basic 9 (Session of State management, simple code of instance shopping cart)

Source: Internet
Author: User
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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.