<%@ 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.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




