博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
结合MVC.NET相关知识实现在线卖酒销售项目(四)
阅读量:7046 次
发布时间:2019-06-28

本文共 13013 字,大约阅读时间需要 43 分钟。

结合项目三内容 本章内容实现”购物车(增删改)“功能

一、在”BrasServer“文件添加”Web窗体“名为”BarShow.aspx“,并编辑前台代码,拖进“Repeater”控件编辑

代码示例:

<%@ Page Title="" Language="C#" MasterPageFile="~/BarsInfo/Bar.master" AutoEventWireup="true" CodeFile="BarShow.aspx.cs" Inherits="BrasServer_BarShow" %>
我的购物车 <%--
  • 图片
  • 名字
  • 价格
  • 数量
  • 删除
--%>
图片 名字 价格 数量 删除
<%#Eval("ProductName") %>
<%#Eval("Price","{0:F1}") %>
删除
总计:
修改数量
提交订单

标题

国外酒名排名

h1.尊尼获加

8000¥

h2.轩尼诗

2000¥

h3.铭悦香槟

9999¥

h4.人头马

2333$
h5.家豪威士忌
8999$
h6.白兰地
5600$

酒类 销售

人头马特级 500 台.

轩尼诗XO 200 台.

马爹利 100 箱.

珠江啤酒 520 箱.

老伯威 4023 瓶.

活福珍藏威士忌 1 瓶.

35% Complete (success)
20% Complete (warning)
10% Complete (danger)

好酒推销

信息

类型

徽章

著名酒名作家.

袁军 刘斌灏
艺术 42
.二窝头 1
.白酒 22
.米酒 30
.清酒 412
.啤酒 999

ABSOLUT

享誉国际的顶级烈酒品牌绝对伏特加(ABSOLUT VODKA)在福布斯(Forbes)商业杂志所评选的美国奢侈品牌独占鳌头
所生产的顶级伏特加不但口感圆润,而且质量无与伦比
品牌所体现出来的完美和无穷创造力更是为世界所首肯,从而名扬九州.
View Code

样式代码(针对Repeater控件样式):

* {padding:0;margin:0; font-family:微软雅黑;}body {background-color:#fff;}a {text-decoration:none;}img {border:none;}.clear {clear:both;}.txt { width:280px; height:24px; line-height:24px; font-size:14px; border:1px solid #cc1616; overflow:auto;}.btnSubmit {width:66px; height:26px; line-height:24px; font-size:14px; border:1px solid #cc1616; background:#fbfe1f;color:#cc1616;}.btn {width:66px; height:26px; line-height:24px;font-size:14px; border:1px solid #000; background:#d0d0d0;color:#000;}/*头部*/.header01 {background:url(../images/header01.jpg) repeat-x; width:100%; height:88px; overflow:hidden;}    .header01 h1 {color:#cc1616;margin-left:50px;font-family:微软雅黑;}/*主导航*/.nav {background:url(../images/nav.jpg) repeat-x; width:100%; height:36px; overflow:hidden;}.nav ul { float:right; margin-right:50px;}.nav ul li{list-style:none; float:left; height:36px;margin:0 8px;  text-align:center; line-height:36px;}.nav ul li a {font:14px; color:#fff;padding:0 8px; display:block; width:100%; height:36px;}.nav ul li a:hover,.nav ul li a.hover { background:url(../images/navhover.jpg) repeat-x; }/*主界面*/.maintbl {width:100%; height:100%;border:none; border-collapse:collapse; table-layout:fixed}/*操作页面菜单样式*/.menu { width:1000px; margin:0 auto 8px auto; overflow:hidden; border-bottom:1px solid #cc1616;}.menu .title { background:url(../images/menu01.jpg) repeat-x;position:relative; color:#cc1616;font-size:14px; width:100%; height:27px; line-height:28px; padding-left:8px;}.menu .title img {position:absolute; top:7px;}.menu .title span { margin-left:18px;}.menu .operate { width:100%; height:28px; line-height:28px;}.menu .operate ul {list-style:none; margin-left:8px;}.menu .operate ul li{list-style:none; float:left; margin-left:4px; height:28px; }.menu .operate ul li a {font-size:12px; display:block; padding:0 4px; color:gray;line-height:24px; height:24px; margin-top:4px;}.menu .operate ul li a:hover {color:#fff; background:#cc1616;}.page {width:1000px; margin:8px auto; border:1px solid #999;}/*表单样式*/.form { width:100%; margin-top:20px;}.form ul { margin-left:8px;}.form ul li { list-style:none; margin:8px; border-bottom:1px dashed rgb(204,204,204); padding-bottom:8px;}.form ul li span.error { color:green;font-size:12px;font-family:宋体; margin-left:8px;}.form ul li span.warm { color:gray;font-size:12px;font-family:宋体;margin-left:8px;}/*数据表格*/.datatbl { width:100%; margin-top:10px;}.datatbl table {width:980px; margin:10px auto;border:2px solid rgb(204,204,204); border-collapse:collapse;}.datatbl table tr th { height:26px; background:url(../images/tblth.jpg) repeat-x; border:1px solid rgb(204,204,204);font-size:14px;}.datatbl table tr td { height:26px; border:1px solid rgb(204,204,204); text-align:center;font-size:14px;}.datatbl table tr td a { color:gray;}.datatbl table tr td a:hover {color:#cc1616;}/*分页空间*/.pager {width:980px; margin:10px auto;}.pager ul { list-style:none; float:right; margin-right:8px;}.pager ul li {float:left; margin-left:8px;font-size:12px; height:22px; line-height:22px;}.pager ul li select { height:22px; text-align:center;}.pager ul li a {display:block; font-size:12px;border:1px solid #999; color:#999; padding:0 6px; height:20px; line-height:20px;}.pager ul li a:hover { border:1px solid #cc1616; color:#fff; background:#cc1616;}/*查询条件*/.search { width:980px; margin:10px auto; height:24px;}/*导航菜单*/.leftnav {margin-top:10px;width:190px;}.leftnav ul {list-style:none; margin:8px  0 0 16px; width:150px;}.leftnav ul li {margin-bottom:8px;position:relative;width:150px;}.leftnav ul li img { position:absolute; top:4px;}.leftnav ul li a {font-size:14px; color:#000; margin-left:18px;}.leftnav ul li a:hover,.leftnav ul li a.hover {font-size:14px; color:#cc1616;font-weight:bold;}
View Code

 

二、在“Models”添加一个类名为“ProdItem.cs”

 如图所示:

 

代码示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Models{  public  class ProdItem    {        public int ProductId { get; set; }        public string ProductName { get; set; }        public decimal Price { get; set; }        public int Quantity { get; set; }    }    }
View Code

 

三、在“BraSys”添加一个“ASP.Code”文件夹,在“ASP.Code”添加一个类名为“Cart.cs”编辑“增删改”方法

1、如图所示(ASP.Code)

2、如图所示(Cart.cs方法):上

3、如图所示(Cart.cs方法):下

代码示例:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using Models;using BLL;/// /// Cart 的摘要说明/// public class Cart{    public Cart()    {        //        // TODO: 在此处添加构造函数逻辑        //    }    private Dictionary
Carts = new Dictionary
(); public void Add(string id) { ProdItem item = null; //判断产品是否存在 if (Carts.TryGetValue(id, out item)) { item.Quantity++; } else { Bars bs = new BarManager().Xyy_ProductGetModel(id); item = new ProdItem { ProductId = bs.ProductId, ProductName = bs.ProductName, Price = bs.Price, Quantity = 1 }; Carts.Add(id, item); } } //获取购物车数据 public List
CartItems { get { return Carts.Values.ToList(); } } //获取总数 public decimal Total { get { decimal total = 0; foreach (ProdItem item in Carts.Values) total += item.Price * item.Quantity; return total; } } //删除 public void Remove(string itemId) { Carts.Remove(itemId); }}
View Code

 

四、完成后,到“BrasServer/BarShow.aspx”编辑后台代码

1、如图所示(绑定传参方法):

 

2、如图所示(修改数量事件与删除事件方法):

3、如图所示(总计方法):

 

代码示例:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class BrasServer_BarShow : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            Add();            //绑定            BindCart();        }    }    //购物车    private Cart Carts    {        get         {            Cart cart = null;            if (Session["cart"] == null)                cart = new Cart();            else                cart = Session["cart"] as Cart;            return cart;        }        set { Session["cart"] = value; }    }    //添加购物    private void Add()    {        if(Request.QueryString["ProductId"]!=null)        {            //添加购物            Cart cart = Carts;            cart.Add(Request.QueryString["ProductId"]);            Carts = cart;        }    }    //绑定购物车    private void BindCart()    {        repShow.DataSource = Carts.CartItems;        repShow.DataBind();        //总计        lblTotal.Text = Carts.Total.ToString();        Session["cart"] = Carts;    }    //修改事件    protected void lkbUpdateQty_Click(object sender, EventArgs e)    {        HiddenField hid = null;        TextBox tex = null;        foreach (RepeaterItem item in repShow.Items)        {            hid = item.FindControl("hidid") as HiddenField;            tex = item.FindControl("txtQty") as TextBox;            Carts.SetQuantity(hid.Value, Convert.ToInt32(tex.Text));        }        BindCart();    }       //删除事件    protected void repShow_ItemCommand(object source, RepeaterCommandEventArgs e)    {        if (e.CommandName == "Delete")        {            Carts.Remove(e.CommandArgument.ToString());            BindCart();        }    }}
View Code

 五、到“BrasServer/BraClothList.aspx”前台代码编辑“链接”方法

如图所示:

代码示例:

<%@ Page Title="" Language="C#" MasterPageFile="~/BrasServer/Bratheme.master" AutoEventWireup="true" CodeFile="BraClothList.aspx.cs" Inherits="BrasServer_BraClothList" %><%@ Register Src="~/FindControl/BraControl.ascx" TagPrefix="uc1" TagName="BraControl" %>
在线买酒产品
牌子选择
日期
价格
<%--
--%>
<%#Eval("ProductName") %>
价格:<%#Eval("Price","{0:F1}") %>
日期:<%#Eval("PublishDate","{0:yyyy-MM-dd}") %>>
添加购物车
 
上一页
下一页
View Code

 运行效果:

 

转载于:https://www.cnblogs.com/xuyangyang/p/6404704.html

你可能感兴趣的文章
easyui在当前tab中直接加载url
查看>>
线程池原理
查看>>
leetcode-258-Add Digits
查看>>
python 某个目录下的所有文件列表
查看>>
HttpServlet详解
查看>>
转:Android之Tab分页标签的实现方法一-----TabActivity和TabHost的结合
查看>>
菜鸟修炼C语言小设计之——学生成绩管理系统
查看>>
Linux设备驱动工程师之路——设备模型(上)底层模型
查看>>
python基础补充
查看>>
常见图片格式详解
查看>>
关于JavaScript
查看>>
命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“Excel”。是否缺少程序集引用 的另一种解决方案...
查看>>
MySQL技巧(一)
查看>>
python-os创建文件夹-create_dir_if_not_exist.py
查看>>
SpringCloud 进阶之Ribbon和Feign(负载均衡)
查看>>
UITableView:下拉刷新和上拉加载更多
查看>>
CFileDialog
查看>>
centOS Python 安装2.7以上版本
查看>>
最近気になる清掃
查看>>
C语言下的简易计算器
查看>>