net 购物车实现代码参照

using   System; 
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace BookShopOnNet
{
///<summary>
/// showMyBus 的摘要说明。
///</summary>
public partial class showMyBus : System.Web.UI.Page
{

protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//Server.Execute( "judge.aspx ");
if(Session[ "logined "]==null || (bool)Session[ "logined "]==false ||Session[ "userName "]==null)
{
Response.Redirect( "Main.aspx ");
}

if(!this.IsPostBack)
{

if(Session[ "myCartTable "]==null)
{
//说明是第一次,创建一个购物车
createMybus();
addToBus();//添加到购物车
caculator();
}
else
{
addToBus();//直接添加到购物车
caculator();
}
}
}


//创建一个购物车
private void createMybus()
{
DataSet ds = new DataSet();//创建一个数据集
DataTable newDT=new DataTable( "CartTable ");//创建一个表
ds.Tables.Add(newDT);//将表添加到数据集中
DataColumn newDC;//创建表的项

newDC=new DataColumn( "bookID ",System.Type.GetType( "System.String "));
ds.Tables[ "CartTable "].Columns.Add(newDC);//将这一项添加到数据集中


newDC=new DataColumn( "bookName ",System.Type.GetType( "System.String "));
ds.Tables[ "CartTable "].Columns.Add(newDC);


newDC=new DataColumn( "author ",System.Type.GetType( "System.String "));
ds.Tables[ "CartTable "].Columns.Add(newDC);

newDC=new DataColumn( "publish ",System.Type.GetType( "System.String "));
ds.Tables[ "CartTable "].Columns.Add(newDC);

newDC=new DataColumn( "bookCount ",System.Type.GetType( "System.Int32 "));
newDC.DefaultValue=1;
ds.Tables[ "CartTable "].Columns.Add(newDC);

newDC=new DataColumn( "price ",System.Type.GetType( "System.Double "));
ds.Tables[ "CartTable "].Columns.Add(newDC);

newDC=new DataColumn( "totalPrice ",System.Type.GetType( "System.Double "));
newDC.DefaultValue=0;
ds.Tables[ "CartTable "].Columns.Add(newDC);

Session[ "myCartTable "]=newDT;
this.dgidMyBus.DataSource=ds.Tables[ "CartTable "].DefaultView;
this.dgidMyBus.DataKeyField= "bookID ";
this.dgidMyBus.DataBind();
}

//添加信息到购物车
private void addToBus()
{
string bookId=Request.QueryString[ "bookId "].ToString();

DataTable nowTable=new DataTable( "nowCartTable ");//创建一个新的表
nowTable=(DataTable)Session[ "myCartTable "];//从会话中得到购物车的内容
int pn=nowTable.Rows.Count;//当前购物车的总行数

int i=0;//作为记数器,记录是否有与现在的购物车内容有相重的
bool hasone=false;//是否相重标志

string nowBookId;
//循环进行查找,找到相重的项
while(i <pn && !hasone)
{
nowBookId=nowTable.Rows[i][0].ToString();
if(nowBookId==bookId)
{
hasone=true;//表示找到
}
else
{
i++;
}
}

if(hasone)
{
//如果有相同的数据项,则更改数据行
DataRow oldDR;//标识相同的那一行
oldDR=nowTable.Rows[i];
oldDR[ "bookCount "]=Int32.Parse(oldDR[ "bookCount "].ToString())+1;//数量增1
oldDR[ "totalPrice "]=Int32.Parse(oldDR[ "bookCount "].ToString())*Double.Parse(oldDR[ "price "].ToString());
}
else if(bookId!= "null ")
{
//没有该项,则应该添加一行
DataRow newDR;//定义一个新行
double unitp;//单价

SqlConnection con=DB.createConnection();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand( "select * from myBookInfo where bookID= ' "+bookId+ " ' ",con);
DataSet ds=new DataSet();//定义一个数据集
sda.Fill(ds, "addItem ");//真充
newDR=nowTable.NewRow();//添加作为表的一行

newDR[0]=bookId;
newDR[1]=ds.Tables[ "addItem "].Rows[0][ "bookName "].ToString();
newDR[2]=ds.Tables[ "addItem "].Rows[0][ "author "].ToString();
newDR[3]=ds.Tables[ "addItem "].Rows[0][ "publish "].ToString();
unitp=Double.Parse(ds.Tables[ "addItem "].Rows[0][ "price "].ToString());//价格

newDR[5]=unitp;//单价
newDR[6]=unitp;//总价
nowTable.Rows.Add(newDR);//加载该行

}

this.dgidMyBus.DataSource = nowTable.DefaultView;
this.dgidMyBus.DataKeyField= "bookID ";
this.dgidMyBus.DataBind();//重新绑定

Session[ "myCartTable "] = nowTable; //更新会话
}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///</summary>
private void InitializeComponent()
{
this.dgidMyBus.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgidMyBus_DeleteCommand);
this.dgidMyBus.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgidMyBus_ItemDataBound);

}
#endregion

private void dgidMyBus_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//执行删除操作
string bookId=this.dgidMyBus.DataKeys[e.Item.ItemIndex].ToString();//得到当前选中列主键
//对行进行更新
DataTable nowTable=new DataTable( "nowCartTable ");//创建一个新的表
nowTable=(DataTable)Session[ "myCartTable "];//从会话中得到购物车的内容
int pn=nowTable.Rows.Count;//当前购物车的总行数

int i=0;//作为记数器,记录是否有与现在的购物车内容有相重的
bool hasone=false;//是否相重标志
string nowBookId;
//循环进行查找,找到相重的项
while(i <pn && !hasone)
{
nowBookId=nowTable.Rows[i][0].ToString();
if(nowBookId==bookId)
{
hasone=true;//表示找到
}
else
{
i++;
}
}
nowTable.Rows[i].Delete();
this.dgidMyBus.DataSource = nowTable.DefaultView;
this.dgidMyBus.DataKeyField= "bookID ";
this.dgidMyBus.DataBind();//重新绑定
Session[ "myCartTable "] = nowTable; //更新会话
caculator();
}

private void dgidMyBus_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//实现高度亮起
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add( "onmouseover ", "c=this.style.backgroundColor;this.style.backgroundColor= '#6699ff ' ");
e.Item.Attributes.Add( "onmouseout ", "this.style.backgroundColor=c ");
//对是否删除进行提示
((LinkButton)e.Item.Cells[6].Controls[0]).Attributes.Add( "onclick ", "return confirm( '确认要删除吗? ') ");
}
}

protected void btnContinue_Click(object sender, System.EventArgs e)
{
Response.Redirect( "showAll.aspx?Id=all ");//定位到显示全部图书的信息
}
private void caculator()
{
//计算总价钱
if(Session[ "myCartTable "]!=null)
{
//如果购物车不为空
int i;
Double TotalPri;
TotalPri=0;//总价钱初始为零
DataTable nowTable=new DataTable( "nowCartTable ");
nowTable=(DataTable)Session[ "myCartTable "];
if(nowTable.Rows.Count> 0) //返回购物车中是否有货物
{
for(i=0;i <=nowTable.Rows.Count-1;i++)
{
TotalPri=TotalPri+Double.Parse(nowTable.Rows[i][6].ToString());//Double.Parse((string)TotalText.Text);

}
this.lbltotalPrice.Text= "总计: "+TotalPri.ToString()+ "";
}
else
{
this.lbltotalPrice.Text= " ";

}
}

}

protected void btnOrderList_Click(object sender, System.EventArgs e)
{
Response.Redirect( "orderList.aspx ");
}

protected void dgidMyBus_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}

 

转载于:https://www.cnblogs.com/yifenghong/articles/2285501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值