ASP. NET 1. Shopping Cart created with pure asp.net + cookie

Source: Internet
Author: User

Disadvantage: Use the return button in IE even if the global cache is used
 
(1) Add Global. asax, add the following content to it, and set Global non-Cache
?
Protected void Application_BeginRequest (Object sender, EventArgs e)
{
HttpContext. Current. Response. Cache. SetNoStore ();
}
(2) create a product sales page
 
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default8.aspx. cs" Inherits = "Default8" %>

<! 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> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: DataList ID = "DataList1" runat = "server" RepeatDirection = "Horizontal" RepeatColumns = "4" AlternatingItemStyle-Wrap = "true" ItemStyle-Width = "100">
<ItemTemplate>
<Asp: Image ID = "Image2" runat = "server" ImageUrl = '<% # DataBinder. Eval (Container. DataItem, "product_pic") %>'/> <br>
<Asp: Label ID = "Label1" runat = "server" Text = '<% # DataBinder. eval (Container. dataItem, "product_name") %> '> </asp: Label> <br>
<Asp: Label ID = "Label2" runat = "server" Text = '<% # DataBinder. eval (Container. dataItem, "product_price") %> '> </asp: Label> <br>
<Asp: Label ID = "Label3" runat = "server" Visible = "false" Text = '<% # DataBinder. eval (Container. dataItem, "product_id") %> '> </asp: Label> <br>
<Asp: TextBox ID = "TextBox1" runat = "server" Width = '35' MaxLength = "3" Text = "1"> 1 </asp: textBox> & nbsp;
<Asp: LinkButton ID = "LinkButton1" runat = "server" OnCommand = "button#command" CommandName = '<% # DataBinder. eval (Container. dataItem, "product_id") %> '> purchase </asp: LinkButton>
</ItemTemplate>
</Asp: DataList> </div>
<Asp: Button ID = "Button1" runat = "server" Text = "clear shopping cart" onclick = "button#click"/>
</Form>
<P> <a href = "shopcar. aspx"> my shopping cart (up to 20 pieces of data) </a> </p>

</Body>
</Html>
 
Add code to the product sales page:
 
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 Default8: System. Web. UI. Page
{
Database db = new database ();
HttpCookie cok = null;

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
This. DataList1.DataSource = db. select_db ("select * from product_list ");
This. DataList1.DataBind (); // Convert. ToString ();
}

}
Protected void button#command (object sender, CommandEventArgs e)
{
// Number of products marked by www.2cto.com
Int num = 1;
LinkButton lb = (LinkButton) sender;
String str = lb. ClientID;
String [] str2 = str. Split ('_');
Str2 [1] = str2 [1]. Substring (3, str2 [1]. Length-3 );
Int I = int. Parse (str2 [1]);
If (TextBox) this. DataList1.Items [I]. FindControl ("TextBox1"). Text. Trim (). Length! = 0)
{
Num = int. Parse (TextBox) this. DataList1.Items [I]. FindControl ("TextBox1"). Text );
}

If (Request. Cookies ["CarInfo"] = null)
{
Cok = new HttpCookie ("CarInfo ");
Cok. Expires = DateTime. Now. AddDays (2 );
Response. AppendCookie (cok );
}

Cok = Request. Cookies ["CarInfo"];

If (cok. Values. Count> = 20)
{
Response. Write ("the shopping cart is full! ");
}

// Check whether this item exists in the shopping cart. If so, accumulate
If (cok. Values [e. CommandName] = null)
{
Cok. Values. Add (e. CommandName, num. ToString ());
}
Else
{
Cok. Values [e. CommandName] = int. Parse (cok. Values [e. CommandName]. ToString () + num + "";
}

Cok. Expires = DateTime. Now. AddDays (2 );
Response. AppendCookie (cok );

}
Protected void button#click (object sender, EventArgs e)
{
If (Request. Cookies ["CarInfo"] = null)
{
Return;
}

Cok = Request. Cookies ["CarInfo"];
Cok. Expires = DateTime. Now. AddDays (-1 );
Response. AppendCookie (cok );
}
}
 
(3) create a shopping cart page
 
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "shopcar. aspx. cs" Inherits = "shopcar" %>

<! 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> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div> <asp: Label ID = "car_msg" runat = "server" Text = ""> </asp: Label>
</Div>
<Div>
<Asp: Repeater ID = "Repeater1" runat = "server">
<HeaderTemplate> <table border = "1"> </HeaderTemplate>
<ItemTemplate>
<Tr>
<Td> <asp: CheckBox ID = "CheckBox1" runat = "server" Checked = '<% # DataBinder. eval (Container. dataItem, "select") %> '/> </td>
<Td> <asp: Label ID = "product_id" runat = "server" Text = '<% # DataBinder. eval (Container. dataItem, "id") %> '> </asp: Label> </td>
<Td> <% # DataBinder. Eval (Container. DataItem, "name") %> </td>
<Td> <% # DataBinder. Eval (Container. DataItem, "num") %> </td>
<Td> <asp: LinkButton ID = "LinkButton1" runat = "server" CommandName = '<% # DataBinder. eval (Container. dataItem, "id") %> 'oncommand = "LinkButton2_Command"> Delete </asp: LinkButton> </td>
</Tr>
</ItemTemplate>
<FooterTemplate> </table> </FooterTemplate>
</Asp: Repeater>
</Div>
</Form>
</Body>
</Html>
 
Add code to the shopping cart page:
 
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;

Public partial class shopcar: System. Web. UI. Page
{
HttpCookie cok = null;
DataTable dt = new DataTable ();
DataTable dt2 = new DataTable ();
Database db = new database ();

Protected void Page_Load (object sender, EventArgs e)
{
This. car_msg.Text = "the shopping cart is empty ";

If (Request. Cookies ["CarInfo"]! = Null)
{
Dt2.Columns. Add ("select", typeof (bool ));
Dt2.Columns. Add ("id ");
Dt2.Columns. Add ("name ");
Dt2.Columns. Add ("num ");
Cok = Request. Cookies ["CarInfo"];

This. car_msg.Text = "Total" + Request. Cookies ["CarInfo"]. Values. Count + "" + "shopping records ";

// When you press the return key incorrectly
// Items in cok do not match
// This error occurs only when the shopping cart is deleted.
If (cok. Values [0]. Length = 0)
{
Cok. Expires = DateTime. Now. AddDays (-1 );
Response. AppendCookie (cok );
This. car_msg.Text = "the shopping cart is empty ";
This. Repeater1.DataBind ();
Return;
}

// Because the cookie does not store the product name, you need to query it once in the database.
String SQL = "select * from product_list where ";
For (int I = 0; I <cok. Values. Count-1; I ++)
{
SQL = SQL + "product_id = '" + cok. Values. Keys [I] + "' or ";
}

SQL = SQL + "product_id = '" + cok. Values. Keys [cok. Values. Count-1] + "'";

Dt = db. select_db (SQL );

// Add the query result to the new dt2 and bind it
For (int I = 0; I <cok. Values. Count; I ++)
{
Dt2.Rows. Add (false, cok. Values. Keys [I], dt. Rows [I] [1]. ToString (), cok. Values [I]);
}

// Response. Write (SQL );

This. Repeater1.DataSource = dt2;
This. Repeater1.DataBind ();
}
}
Protected void LinkButton2_Command (object sender, CommandEventArgs e)
{
If (Request. Cookies ["CarInfo"] = null)
{
Return;
}

// After the cookie is deleted, remember to delete the data in dt2.
Try
{
Cok = Request. Cookies ["CarInfo"];
Cok. Values. Remove (e. CommandName );
Response. AppendCookie (cok );
Dt2.Select ("id = '" + e. CommandName + "'") [0]. Delete ();
}
Catch
{
// Return;
}

This. Repeater1.DataSource = dt2;
This. Repeater1.DataBind ();

If (dt2.Rows. Count> 0)
{
This. car_msg.Text = "Total" + Request. Cookies ["CarInfo"]. Values. Count + "" + "shopping records ";
}
Else
{
This. car_msg.Text = "the shopping cart is empty ";
}
}
}
 


 

From South-South Space

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.