使用hashtable实现的购物车
发表于:2007-06-30来源:作者:点击数:
标签:
%@ Page Language= VB ContentType=text/html De bug =true% %@ import namespace=system.data % %@ import namespace=system.data.oledb % html head meta http-equiv=Content-Type content=text/html; charset=gb2312 title无标题文档/title style type=tex
<%@ Page Language="
VB" ContentType="text/html" De
bug="true"%>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<style type="text/css">
<!--
td {
font-size: 9pt;
line-height: 24px;
}
-->
</style>
<style type="text/css">
<!--
a:link {
color: #000000;
text-decoration: none;
}
a:hover {
color: #FF0000;
text-decoration: none;
}
a:visited {
text-decoration: none;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<form runat="server">
<!--#include virtual="header.htm"-->
<table width="776" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="159"> </td>
<td width="617"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="17%" height="26"><p> </p>
<p>您所选购的商品</p></td>
<td width="2%" height="26"> </td>
<td width="81%" height="26"> </td>
</tr>
<tr>
<td height="26"> </td>
<td height="26"> </td>
<td height="26"><asp:Datagrid ID="dgCar" runat="server"
autogeneratecolumns="false"
BorderColor="#000000"
BorderWidth="1"
CellPadding="3"
HeaderStyle-BackColor="#990000"
HeaderStyle-ForeColor="#FFFFFF"
DataKeyField="title_id"
>
<columns>
<asp:boundcolumn HeaderText="商品" DataField="title" ItemStyle-Width="300"></asp:boundcolumn>
<asp:templatecolumn HeaderText="数量">
<itemtemplate>
<asp:textbox size=1 id="Qty" runat="server"
text=‘’<%# Databinder.eval(container.dataitem,"qty")%>‘’/>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn HeaderText="单价" DataField="price" DataFormatString="{0:c}"></asp:boundcolumn>
<asp:boundcolumn HeaderText="总计" DataField="total" DataFormatString="{0:c}"></asp:boundcolumn>
<asp:templatecolumn HeaderText="删除">
<itemtemplate>
<asp:checkbox runat="server" ID="del"/>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:Datagrid>
<asp:label runat="server" ID="msg" text="购物车为空" />
<hr>
<div align="right">总计:
<asp:label ID="lbltotal" runat="server" text="0"></asp:label>
<br>
折扣:<asp:label ID="sale" runat="server" Text="0.9"></asp:label><br>
应付:<asp:label ID="realprice" runat="server" Text="0" /></div>
<div align="center"><br>
<asp:imagebutton ID="re_car" ImageUrl="images/cxjs.gif" runat="server" OnClick="re_car_click" />
<asp:imagebutton ID="buy" ImageUrl="images/qsyt.gif" runat="server" />
<asp:imagebutton ID="clear" ImageUrl="images/qk.gif" runat="server" OnClick="clear_click" />
<a href="display_all.aspx">继续购物</a></div></td>
</tr>
<tr>
<td height="26"> </td>
<td height="26"> </td>
<td height="26"> </td>
</tr>
</table></td>
</tr>
</table>
<!--#include virtual="footer.htm"-->
</form>
</body>
</html>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
if not ispostback then
‘’取得用户要加入购物书的商品的ID
Dim product as string = request.QueryString("title-id")
‘’判断如果是用户所购买的第一件商品的话则那立一个hashTable
‘’否则从session中将已经存在的hashTable取回
Dim Hash As HashTable
IF Session("car") is nothing then
hash = New hashTable
Else
Hash = Session("car")
End if
‘’判断用户所选购的商品是否已经存在于购物车中
‘’如果不存在则将其加入购物车,如果存在则在原有数据上加1
if Not Hash.ContainsKey(product) then
Hash.Add(product,1)
else
hash.item(product) +=1
end if
session("car")=hash
display()
end if
End Sub
‘’------------------用于显示商品的子程序--------------------
Sub Display()
Dim i as integer
dim x as string
‘’取得session中的hashTable
dim hash as hashtable
Hash = Session("car")
‘’将所有选购的商品构造成为一个字符串
dim Products As String
products = "(‘’"
For Each X in hash.keys
Products = products & X & "‘’,‘’"
next
Products = Products & "‘’)"
‘’根据所购商品的字符串读取商品信息,然后放在producttable中
dim conn as oledbconnection
Dim ds as new dataset
dim Table1 as DataTable
dim connstr as string = "provider=Microsoft.jet.oledb.4.0;data source=" & server.MapPath("db3.mdb")
conn = new oledbconnection(connstr)
conn.open()
‘’读取数据并将数据填充到table1中
dim comm as oledbdataadapter
dim strsql as string ="select title_id,title,price from titles where title_id in " & products
comm = new oledbdataadapter(strsql,conn)
comm.fill(ds,"product")
table1 = ds.tables("product")
‘’向table1中增加一列
table1.columns.add(new datacolumn("qty",gettype(integer)))
‘’将title设置为主键
Dim Keys() as DataColumn = {table1.Columns("title_id")}
table1.primarykey=keys
‘’向producttable表新增字段
for each x in hash.keys
table1.rows.find(x).item("qty")=hash.item(x)
next
‘’向table1中增加总计列,并计算总计值
table1.columns.add(new datacolumn("total",gettype(double),"price*qty"))
‘’判断购物车是否为空
if table1.rows.count =0 then
dgcar.visible="false"
msg.visible="true"
else
dgcar.visible="True"
msg.visible="False"
end if
‘’计算帐单总价及折扣后的价格
lbltotal.text="0"
For i=0 to table1.rows.count-1
lbltotal.text =string.format("{0:c}",cdbl(lbltotal.text)+table1.rows(i).item("total"))
next
realprice.text = string.format("{0:c}",cdbl(sale.text) * lbltotal.text)
‘’将数据绑定dgcar
dgCar.datasource=table1.defaultview
dgCar.databind()
conn.close
End Sub
‘’------------------用于清空购物车的事件程序---------------
Sub clear_click(sender as object,e as imageclickeventargs)
‘’将session("car")清空
session("car") = new hashtable()
lbltotal.text = "0"
realprice.text="0"
display()
End sub
‘’------------------用于更新购物车的事件程序------------------
Sub re_car_click(sender as object,e as imageclickeventargs)
‘’判断Session("car")是否存在,如果在刚将其取得保存在hash中
Dim Hash As HashTable
IF Session("car") is nothing then
hash = New hashTable
Else
Hash = Session("car")
End if
‘’依决检查每一行的数量和checkbox控件,并更新hashtable
Dim qty As Textbox
dim del as checkbox
Dim I as integer
For I = 0 to dgcar.items.count -1
qty = dgcar.items(i).findcontrol("qty")
del = dgcar.items(i).findcontrol("del")
if (cint(qty.text)> 0) and (not del.checked) then
hash(dgcar.datakeys(i))=cint(qty.text)
else
hash.remove(dgcar.datakeys(i))
end if
next
display()
End sub
</script>
原文转自:http://www.ltesting.net