购物车设计与实现(基于数据库).doc

上传人:自*** 文档编号:124916990 上传时间:2020-03-14 格式:DOC 页数:5 大小:163KB
返回 下载 相关 举报
购物车设计与实现(基于数据库).doc_第1页
第1页 / 共5页
购物车设计与实现(基于数据库).doc_第2页
第2页 / 共5页
购物车设计与实现(基于数据库).doc_第3页
第3页 / 共5页
购物车设计与实现(基于数据库).doc_第4页
第4页 / 共5页
购物车设计与实现(基于数据库).doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《购物车设计与实现(基于数据库).doc》由会员分享,可在线阅读,更多相关《购物车设计与实现(基于数据库).doc(5页珍藏版)》请在金锄头文库上搜索。

1、购物车设计与实现(1-基于数据库)一、数据库设计:1.Tcart表结构如下2.Tuser表 (删除了UserID字段,设置UserName为主键)页面设计要点:在该页面开发阶段,为了避免每次都要登陆,可以先在Session中建立UserName变量,赋一个存在的用户名.在ViewBookDetail.aspx和ShopCart.aspx页的Page_load事件中加入下面一行代码:SessionUserName=liqin; /测试用,网站联调时注释掉一、 设计购物车页面 1.用GridView控件显示购物车信息 通过编辑列添加以下字段(注意字段名与Cart类的属性cart中的各个列名一致),

2、以显示购物车信息: BookID BookName Number Price注意:为了实现就地编辑数量,数量Number用“模版”字段, 通过“编辑摸版”对话框,在其中加入一个TextBox用于显示数量,以及两个用于修改数量的按扭.分别设置其text属性为“+”和“-”,并通过“DataBindings”操作将其CommandArgument属性绑定到BookID字段,如图:为了方便页面的编码,再分别设置按扭的CommandName属性与其Text属性相同。二、页面关键编码:/1.在Page_Load事件中加入测试用代码,以模拟用户登录成功protected void Page_Load(ob

3、ject sender, EventArgs e) SessionUserName = liqin; /测试用用户,以免去开发阶段的登录操作 / 2.PreRender为在页面控件已经加载但未呈现前发生的事件,在Page_Load后发生 /在此事件中可以更新购物车中总金额和总数量的显示 protected void Page_PreRender(object sender, EventArgs e) /定义变量:保存购物车中总数量和总金额 int TotalNum=0; Decimal TotalMoney=0; DataTable books; /当前用户的购物车中信息 /获取当前用户的购物

4、车信息 books = TCart.GetCartByUsername(SessionUserName.ToString(); if (books.Rows.Count = 0) Label_num.Text = 你的购物车还是空的,快去选购吧; return; /将结果绑定到GridView上显示 GridView1.DataSource = books; GridView1.DataBind(); /统计购物车中总数量和总金额,两种方法 int num; /for (int i = 0; i books.Rows.Count; i+) / / num = Int32.Parse(books

5、.RowsiNumber.ToString(); /获取数量 / TotalNum += num; / TotalMoney += num * Decimal.Parse(books.RowsiPrice.ToString(); / foreach (DataRow row in books.Rows) num = Int32.Parse(rowNumber.ToString(); TotalNum += num; TotalMoney += num * Decimal.Parse(rowPrice.ToString(); /显示购物车中总数量和总金额 Label_money.Text = 商

6、品总金额(不含运费): + TotalMoney.ToString() + 元; Label_num.Text = 购物车中共有 + books.Rows.Count.ToString()+种商品,总数量为:+TotalNum.ToString() + 件; 3实现购物车的删除功能 因为没有使用数据源控件,需要编写GridView的RowDeleting事件代码: /删除购物车中的一条记录 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) int bookid = Int32.Parse

7、(GridView1.Rowse.RowIndex.Cells0.Text); string UserName= SessionUserName.ToString(); TCart. DeletefromCart(UserName,bookid); 4难点:实现数量的就地编辑功能 /当用户单击表格中的+ - 按扭时,更改数量 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)Try int bookid = Convert.ToInt32(e.CommandArgument.ToStrin

8、g(); /获取BookID /获取用户选择的操作符:2种方法之一 /char op = e.CommandName.ToString()0; /需要设置按扭的CommandName属性为+或- char op = (Button)e.CommandSource).Text0; /直接从按扭的Text属性中获取 TCart.UpdateCart(SessionUserName.ToString(), bookid, op); /清空购物车 protected void LinkButton1_Click(object sender, EventArgs e) TCart.ClearCart(S

9、essionUserName.ToString (); Server.Transfer(/web/Shopcart.aspx); /刷新页面 /结帐 protected void Button3_Click(object sender, EventArgs e) Server.Transfer(/web/AddNewOrder.aspx); 技术要点总结:1.如何响应GridView模板列中的按扭的事件,按扭的CommandAugrment和CommandName属性作用。 (1)是通过 RowCommand 事件操作, 该事件的两个参数的含义是什么?CommandAugrment 获取命令参

10、数 CommandName 获取命令的属性(2)如何获取操作行的键值,如此处的BookID值int bookid = Int32.Parse(GridView1.Rowse.RowIndex.Cells0.Text);(3)如何知道用户单击的是哪个按扭?在GridView1_RowCommand中查看CommandName属性 2.通过“统计购物车中的总金额”,掌握遍历DataTable对象dt的所有行的两种方法:for (int i=0 ; idt.Rows.Count; i+) num = Int32.Parse(Dt.RowsiNumber .ToString())TotalMoney += num * Decimal.Parse(Dt.RowsiPrice.ToString(); /访问特定行、列的方法foreach (DataRow row in dt.Rows) num = Int32.Parse( rowNumber.ToString());TotalMoney += num * Decimal.Parse(rowPrice.ToString(); /访问特定行、列的方法

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 工业设计

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号