ASP.NET购物车实现过程详解
程序员文章站
2023-12-19 18:42:10
1、 将test数据库附加到数据库管理系统中;数据库中的book_info包含下列数据:
2、 新建一个网站,将images文件夹复制到网站中;
3、 在defau...
1、 将test数据库附加到数据库管理系统中;数据库中的book_info包含下列数据:
2、 新建一个网站,将images文件夹复制到网站中;
3、 在default.aspx中,通过datalist控件展示数据库中的所有数据,以行为主序,每行3列,单击购买按钮时,将商品的id和数量保存到hashtable中,并将hashtable放置到session中。
protected void datalist1_itemcommand(object source, datalistcommandeventargs e) { string id = e.commandargument.tostring(); hashtable ht; if (session["shopcar"] == null) { ht = new hashtable(); ht.add(id, 1); session["shopcar"] = ht; } else { ht = (hashtable)session["shopcar"]; if (ht.contains(id)) { int count = int.parse(ht[id].tostring()); ht[id] = count + 1; session["shopcar"] = ht; response.write(count + 1); } else { ht.add(id, 1); session["shopcar"] = ht; } } }
4、 在default.aspx中添加一个超链接,链接到shopcart.aspx,在shopcart.aspx中显示用户购买的商品信息。
提示:
a、在shopcart中先定义下列变量:
hashtable ht; datatable dt; string connstring=@"datasource=.\sqlexpress;initial catalog=test;integrated security=true"; sqlconnection conn; sqlcommand cmd; sqldatareader sdr;
b、页面中添加一个gridview。
c、在page_load中,将dt实例化,建立各列。
protected void page_load(object sender, eventargs e) { dt = new datatable(); datacolumn col = new datacolumn(); col.columnname= "id"; col.datatype =system.type.gettype("system.string"); dt.columns.add(col); col = new datacolumn(); col.columnname= "name"; col.datatype =system.type.gettype("system.string"); dt.columns.add(col); col = new datacolumn(); col.columnname= "num"; col.datatype =system.type.gettype("system.int32"); dt.columns.add(col); col = new datacolumn(); col.columnname= "price"; col.datatype =system.type.gettype("system.single"); dt.columns.add(col); col = new datacolumn(); col.columnname= "total"; col.datatype =system.type.gettype("system.single"); dt.columns.add(col); if (!ispostback) { bind(); } } public void bind() { if (session["shopcar"] == null) { response.write("<script>if(confirm('你没有登录')window.location='default15.aspx';else window.close();</script>"); } else { ht = (hashtable)session["shopcar"]; foreach (object item in ht.keys) { string id = item.tostring(); int num = int.parse(ht[item].tostring()); string sql = "selectbook_name,price from book_info where book_id='" + id + "'"; conn = new sqlconnection(connstring); cmd = new sqlcommand(sql, conn); conn.open(); sdr =cmd.executereader(); if (sdr.hasrows) { sdr.read(); datarow row = dt.newrow(); row["id"] = id; row["num"] = num; row["name"] = sdr.getstring(0); row["price"] =float.parse(sdr[1].tostring()); row["total"] =num*(float.parse(sdr[1].tostring())); dt.rows.add(row); } sdr.close(); conn.close(); } gridview1.datasource = dt.defaultview; gridview1.databind(); } }
d、这时可以看到用户购买的商品,但不能修改数量,也不能删除。
e、添加修改数量,删除商品功能,在aspx页面中定义gridview中的各列:
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"> <columns> <asp:boundfield datafield="id" headertext="id" /> <asp:boundfield datafield="name" headertext="名称" /> <asp:boundfield datafield="price" headertext="价格" /> <asp:templatefield> <itemtemplate> <asp:textbox runat="server" id="textbox1" text='<%# eval("num") %>' ontextchanged="textbox1_textchanged" autopostback="true" ></asp:textbox> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="total" headertext="总计" /> <asp:templatefield> <itemtemplate> <asp:button runat="server" id="button1" commandargument='<%# eval("id") %>' text="删除" onclick="button1_click" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
f、为gridview中的文本框添加textchanged事件:
protected void textbox1_textchanged(object sender, eventargs e) { hashtable ht =(hashtable)session["shopcar"]; if (ht == null) return; for (int i = 0; i < gridview1.rows.count;i++) { string id =gridview1.rows[i].cells[0].text.tostring(); response.write(id); string num = ((textbox)gridview1.rows[i].findcontrol("textbox1")).text; response.write(" "+num+"<br />"); ht[id] = num; } session["shopcar"] = ht; bind(); }
g、为按钮添加单击事件:
protected void button1_click(object sender, eventargs e) { string id = ((button)sender).commandargument; hashtable ht = (hashtable)session["shopcar"]; if (ht == null) return; ht.remove(id); bind(); }
购物车代码:showcart.aspx.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.collections; using system.data; using system.data.sqlclient; public partial class shopcart : system.web.ui.page { hashtable ht; datatable dt; string connstr = "data source=.\\sqlexpress;attachdbfilename=f: \\test.mdf;integrated security=true;connect timeout=30;user instance=true"; sqlconnection conn; sqlcommand cmd; sqldatareader sdr; protected void page_load(object sender, eventargs e) { dt = new datatable(); datacolumn col = new datacolumn(); col.columnname = "id"; col.datatype = system.type.gettype("system.string"); dt.columns.add(col); col = new datacolumn(); col.columnname = "name"; col.datatype = system.type.gettype("system.string"); dt.columns.add(col); col = new datacolumn(); col.columnname = "num"; col.datatype = system.type.gettype("system.int32"); dt.columns.add(col); col = new datacolumn(); col.columnname = "price"; col.datatype = system.type.gettype("system.single"); dt.columns.add(col); col = new datacolumn(); col.columnname = "total"; col.datatype = system.type.gettype("system.single"); dt.columns.add(col); if (!ispostback) { bind(); } } public void bind() { if (session["shopcar"] == null) { response.write("<script>if(confirm('你没有登录')window.location='default.aspx';else window.close();</script>"); } else { ht = (hashtable)session["shopcar"]; foreach (object item in ht.keys) { string id = item.tostring(); int num = int.parse((ht[item].tostring())); string sql = "select book_name,price from book_info where book_id='" + id + "'"; conn = new sqlconnection(connstr); cmd = new sqlcommand(sql, conn); conn.open(); sdr = cmd.executereader(); if (sdr.hasrows) { sdr.read(); datarow row = dt.newrow(); row["id"] = id; row["num"] = num; row["name"] = sdr.getstring(0); row["price"] = float.parse(sdr[1].tostring()); row["total"] = num * (float.parse(sdr[1].tostring())); dt.rows.add(row); } sdr.close(); conn.close(); } } gridview1.datasource = dt.defaultview; gridview1.databind(); } protected void textbox1_textchanged(object sender, eventargs e) { hashtable ht = (hashtable)session["shopcar"]; if (ht == null) return; for (int i = 0; i < gridview1.rows.count; i++) { string id = gridview1.rows[i].cells[0].text.tostring(); response.write(id); string num = ((textbox)gridview1.rows[i].findcontrol("textbox1")).text; response.write(" " + num + "<br />"); ht[id] = num; } session["shopcar"] = ht; bind(); } protected void button1_click(object sender, eventargs e) { string id = ((button)sender).commandargument; hashtable ht = (hashtable)session["shopcar"]; if (ht == null) return; ht.remove(id); bind(); } }
制作一个简单的购物车就是这么简单,大家可以按照我的思路进行创作,在此基础上在添加一些功能。