<%@ page language="c#" codebehind="shoppingcart.aspx.cs" autoeventwireup="false" inherits="myshop.shoppingcart" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>shoppingcart</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link href="mycss.css" type="text/css" rel="stylesheet">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<center>
<form id="form1" runat="server">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:datagrid id="shoppingcartdlt" runat="server" width="500" backcolor="white" bordercolor="black"
showfooter="false" cellpadding="3" cellspacing="0" font-name="verdana" font-size="8pt" headerstyle-backcolor="#cecfd6"
autogeneratecolumns="false" maintainstate="true">
<columns>
<asp:templatecolumn headertext="删除">
<itemtemplate>
<center>
<asp:checkbox id="chkproductid" runat="server" />
</center>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn datafield="prodid" headertext="id" />
<asp:boundcolumn datafield="proname" headertext="商品名称" />
<asp:boundcolumn datafield="unitprice" headertext="单价" />
<asp:templatecolumn headertext="数量">
<itemtemplate>
<asp:textbox id="counttb" runat="server" text='<%#databinder.eval(container.dataitem,"prodcount")%>'>
</asp:textbox>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn datafield="totalprice" headertext="小计(元)" />
</columns>
</asp:datagrid></td>
</tr>
</table>
<br>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><asp:button id="update" runat="server" text="更新我的购物车" cssclass="button2" /></td>
<td><asp:button id="checkout" runat="server" text="结算" cssclass="button5" /> <input type="button" name="close2" value="继续购物" onclick="window.close();return false;"
class="button2"></td>
<td align="right"><br>
<asp:label id="label" runat="server" width="100px" visible="true" forecolor="#ff8080" height="18px"></asp:label></td>
</tr>
</table>
</form>
</center>
</body>
</html>
=======================================================================================
以上为html页面部分
==========================================================================================
using system;
using system.collections;
using system.componentmodel;
using system.web.sessionstate;
using system.web;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.data;
using system.data.oledb;
using system.configuration;
namespace myshop
{
/// <summary>
/// shoppingcart 的摘要说明。
/// </summary>
public class shoppingcart : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid shoppingcartdlt;
protected system.web.ui.webcontrols.button update;
protected system.web.ui.webcontrols.button checkout;
protected system.web.ui.htmlcontrols.htmlform form1;
protected system.web.ui.webcontrols.label label;
protected system.web.ui.webcontrols.checkbox chkproductid;
protected system.web.ui.webcontrols.textbox txtcount;
protected system.web.ui.webcontrols.textbox counttb;
string addproid;
private void page_load(object sender, system.eventargs e)
{
try
{
if (session["logon"]!="yes"||session["username"]==null)
{
response.redirect("error.htm") ;
}
}
catch
{
response.redirect("error.htm") ;
} /////////////查看用户是否已经登陆。
if(!ispostback)
{
if(request.params["mode"]=="view") //检测是否为直接查看购物车。
{
viewshoppingcart();
caculator();
}
if(request.params["productid"]!=null||request.params["productid"]!="")
{
addproid=request["productid"];
updateshoppingcart();
caculator();
}
}// 在此处放置用户代码以初始化页面
}
public void createcarttable() //创建购物车
{
dataset ds = new dataset();
datatable newdt=new datatable("carttable");
ds.tables.add(newdt);
datacolumn newdc;
newdc=new datacolumn("prodid",system.type.gettype("system.int32"));
ds.tables["carttable"].columns.add(newdc);
newdc=new datacolumn("prodcount",system.type.gettype("system.int32"));
newdc.defaultvalue=1;
ds.tables["carttable"].columns.add(newdc);
newdc=new datacolumn("proname",system.type.gettype("system.string"));
ds.tables["carttable"].columns.add(newdc);
newdc=new datacolumn("unitprice",system.type.gettype("system.double"));
ds.tables["carttable"].columns.add(newdc);
newdc=new datacolumn("totalprice",system.type.gettype("system.double"));
ds.tables["carttable"].columns.add(newdc);
newdc=new datacolumn("isdeleted",system.type.gettype("system.int32"));
newdc.defaultvalue=0; // public void writeshoppingcart() 中 newdr[5]="0"; 行,已被注销,
ds.tables["carttable"].columns.add(newdc);
session["mycarttable"]=newdt;
shoppingcartdlt.datasource=ds.tables["carttable"].defaultview;
shoppingcartdlt.databind();
}
public void updateshoppingcart()
{
if(session["mycarttable"]==null)//session["mycarttable"]==null
{
createcarttable(); //调用函数createcarttable()新建一个datatable
writeshoppingcart();
}
else
{ //如果购物蓝中已有商品,则需要对购物信息表datatable进行更新,并将其棒定到shoppingcartdlt
writeshoppingcart();
}
}
public void viewshoppingcart() //查看购物车
{
if(session["mycarttable"]!=null)
{
datatable viewtable=new datatable("nowcarttable");
viewtable=(datatable)session["mycarttable"];
shoppingcartdlt.datasource = viewtable.defaultview; //购物车棒定到shoppingcartdlt
shoppingcartdlt.databind();
}
}
public void writeshoppingcart()
{
if(request.params["mode"]!="view") //检查是否是直接查看购物车,如果直接查看,就不再写mycarttable
{
datatable nowtable=new datatable("nowcarttable");
nowtable=(datatable)session["mycarttable"];
int pn=nowtable.rows.count;
int i=0;
bool hasone=false;
int nowprodid;
while(i<pn && !hasone)
{
nowprodid=int32.parse(nowtable.rows[i][0].tostring());
if(nowprodid==int32.parse(addproid)) //判断购物信息表中,是否存有当前放入商品。 if(nowprodid==int32.parse(addproid))
{
hasone=true;
}
else
{
i++;
}
}
if(hasone)
{ //如果已有该商品,则 hasone=true,更改该数据行
datarow olddr;
olddr=nowtable.rows[i];
olddr["prodcount"]=int32.parse(olddr["prodcount"].tostring())+1;
olddr["totalprice"]=int32.parse(olddr["prodcount"].tostring())*double.parse(olddr["unitprice"].tostring());
}
else
{ //如果没有该商品,在表中新加如一行。
datarow newdr;
double unitp;
string strcon="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(configurationsettings.appsettings["mdbpath2"])+";";
oledbconnection myconnection = new oledbconnection(strcon);
string strsql= "select * from pro where product_id="+addproid+"";
oledbdataadapter mycommand = new oledbdataadapter(strsql, myconnection);
dataset ds = new dataset();
mycommand.fill(ds, "addp");
newdr=nowtable.newrow();
newdr[0]=addproid;
newdr[2]=ds.tables["addp"].rows[0]["product_name"].tostring();
unitp=double.parse(ds.tables["addp"].rows[0]["product_memprice"].tostring()); //会员价
newdr[3]=unitp;
newdr[4]=unitp; //第一次读库,所以总价格和单价是一样的。
//newdr[5]="0";
nowtable.rows.add(newdr);
myconnection.close();
}
shoppingcartdlt.datasource = nowtable.defaultview; //将更新后的 datatable棒定到shoppingcartdlt
shoppingcartdlt.databind();
session["mycarttable"] = nowtable;
//重新保存更新过的datatable
}
}
public void caculator()
{
if(session["mycarttable"]!=null) //购物车是否为空
{
int h;
double totalpri;
totalpri=0;
datatable nowtable3=new datatable("nowcarttable3");
nowtable3=(datatable)session["mycarttable"];
if(nowtable3.rows.count>0) //返回购物车中是否有货物
{
for(h=0;h<=nowtable3.rows.count-1;h++)
{
totalpri=totalpri+int32.parse(nowtable3.rows[h][4].tostring());//double.parse((string)totaltext.text);
}
label.text="总计: "+totalpri.tostring()+" 元" ;
}
}
}
public void update()
{
int i;
int j;
int k;
arraylist deleteitem = new arraylist(10);
datagriditem _item ;
j=0;
int deleteid;
k=0;
datatable nowtable2=new datatable("nowcarttable2");
nowtable2=(datatable)session["mycarttable"];
for(i=0;i<=this.shoppingcartdlt.items.count-1;i++)
{
_item = this.shoppingcartdlt.items[i];
textbox counttext=(textbox)this.shoppingcartdlt.items[i].cells[4].findcontrol("counttb");//controls[1];//_item.findcontrol("counttb");
checkbox productidcheck =(checkbox) _item.findcontrol("chkproductid");
nowtable2.rows[i][1] = int32.parse(counttext.text.tostring());
nowtable2.rows[i][4] = int32.parse(nowtable2.rows[i][1].tostring()) * double.parse(nowtable2.rows[i][3].tostring());
if(productidcheck.checked)
{
nowtable2.rows[i][5] = 1;//添加删除标记1
j=j+1;
}
}
string strexpr="isdeleted>0"; //http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/cpref/html/frlrfsystemdatadatatableclassselecttopic.asp
datarow[] foundrows = nowtable2.select(strexpr);
for(int m = 0; m < foundrows.length; m ++)
{
//console.writeline(foundrows[i][0]);
foundrows[m].delete();
}
shoppingcartdlt.datasource = nowtable2.defaultview;
shoppingcartdlt.databind();
session["mycarttable"] = nowtable2;
caculator();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.update.click += new system.eventhandler(this.update_click);
this.checkout.click += new system.eventhandler(this.checkout_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void update_click(object sender, system.eventargs e)
{
update();
}
private void checkout_click(object sender, system.eventargs e)
{
update();
response.redirect("checkout.aspx");
}
}
}
新闻热点
疑难解答
图片精选