在WebForm中实现购物车思路

关于网站购物车的实现的思考

 

写在前面的话:刚来公司的的时候,老大安排了一个任务,企业站,但是需要实现购物车的功能,以前没做过,所有就向周围的人请教了一下如何实现购物车,自己也在网上搜了一下,有了些自己的认识,于是写了下来

1、实现思路:

在网上查了一下资料,以及向身边请教之后发现,对于网站购物车的实现大体分为三种方法:Session实现、Cookie实现、数据库实现,其实这三种实现,指的只是如何跟踪用户的操作,即用户购买物品,加入购物车,加入了什么物品,加入了多少物品等信息的暂时保存。

这三种方法的不同之处就在于保存用户操作的方式不同,其中Session现在用的不多,由于Session的生命周期,在浏览器关闭时会失效,所以容易使数据丢失,如果用户在浏览网站是不小心关闭了浏览器,当用户再次打开时,加入购物车的物品就会丢失,所以不可取。

Cookie的实现方式是在用户点击加入购物车之后将数据以Cookie的形式存储的客户端,每次用户登录该网站时,首先从Cookie中读取数据出来,这种方式数据库不易丢失,读取速度也快。

数据库的实现方式是最安全的,但是这种方式也是最占用服务器资源的,而且当数据量较大时,会影响服务器响应速度。

2、我选择的方式:

我初步设想是在物品展示页面中做成类似淘宝网、当当网的那种展示页面,在每个物品下面放置一个加入购物车按钮,希望在用户浏览该页面时,点击该按钮将自己喜欢的物品加入购物车。

页面组成:ProductList.aspx、ProductShow.aspx、ShoppingCart.aspx

ProductList.aspx.cs文件

public partial class ProductList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 绑定数据源到Repeater控件
        Bind();
    }

    public void Bind()
    {
        string sqlStr = "select * from product";
        rptProductList.DataSource = new BLL().GetDataSet(sqlStr);
        rptProductList.DataBind();
    }
    protected void btnGoCart_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShoppingCart.aspx");
    }
}
复制代码
public partial class ProductList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 绑定数据源到Repeater控件
        Bind();
    }

    public void Bind()
    {
        string sqlStr = "select * from product";
        rptProductList.DataSource = new BLL().GetDataSet(sqlStr);
        rptProductList.DataBind();
    }
    protected void btnGoCart_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShoppingCart.aspx");
    }
}
复制代码

此页面的功能是:从后台将数据库已有的商品取出来,并绑定到服务器控件上,展示出来,当用户看到自己喜欢的物品时,点击图片,将会跳转到物品详情页,即ProductShow.aspx页面。

由于自己只是做个测试,所以就用自己最喜欢的动漫人物来做图片了,页面运行效果如图:

ProductShow.aspx.cs文件

public partial class ProductShow : System.Web.UI.Page
{
    public string id = "";
    public string picture = "";
    public string name = "";
    private Product product = new Product();

    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["id"].ToString();
        picture = Request.QueryString["picture"].ToString();
        product.Id = this.id;
        product.Name = this.name;
        product.Picture = this.picture;
    }
    /// <summary>
    /// 添加数据到cookies
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddCart_Click(object sender, EventArgs e)
    {
        Cart cart = new Cart();
        cart.WriteCookies(product);
        Response.Redirect("ShoppingCart.aspx");
    }
}
复制代码
public partial class ProductShow : System.Web.UI.Page
{
    public string id = "";
    public string picture = "";
    public string name = "";
    private Product product = new Product();

    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["id"].ToString();
        picture = Request.QueryString["picture"].ToString();
        product.Id = this.id;
        product.Name = this.name;
        product.Picture = this.picture;
    }
    /// <summary>
    /// 添加数据到cookies
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddCart_Click(object sender, EventArgs e)
    {
        Cart cart = new Cart();
        cart.WriteCookies(product);
        Response.Redirect("ShoppingCart.aspx");
    }
}
复制代码

此页面的功能是:首先读取从物品展示页面传过来的物品Id和图片地址,之后便将该物品详情展示在页面中,由于只是测试,所以只传了Id和图片地址,我在考虑是如果需要想当当那样的物品详情页那样展示很多信息的话,是该在此页面根据Id从数据库读取呢,还是应该从物品展示页面传过来呢?如果有知道的,还请留言告诉我,先谢过了。

在此页面中有一个加入购物车按钮,当点击该按钮时,会将该物品加入购物车,在此实现是写入Cookie,写入的规则是,购物车是否存在,存在则再先查看Cookie中是否已有该物品,如果有则增加数量即可,如果没有,则在Cookie中新建一个键值对存入Cookie,如果购物车不存在,则先创建购物车即可,下面是购物车类的WriteCookies代码:

#region 写入cookie
    /// <summary>
    /// 写入cookie
    /// </summary>
    /// <param name="product">商品</param>
    /// <param name="num">个数</param>
    /// <param name="expires">有效期</param>
    public void WriteCookies(Product product, int num = 1, int expires = 15)
    {
        if (System.Web.HttpContext.Current.Request.Cookies["Cart"] != null)
        {
            string cookieValue = System.Web.HttpContext.Current.Request.Cookies["Cart"].Value;
            // 商品不存在
            if (System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()] == null)
            {
                cookieValue = cookieValue + "&" + product.Id + "=" + num.ToString();
            }
            // 商品存在
            else
            {
                int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()].ToString()) + num;
                System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()] = num1.ToString();
                cookieValue = System.Web.HttpContext.Current.Request.Cookies["Cart"].Value;
            }
            HttpCookie cookie = new System.Web.HttpCookie("Cart", cookieValue);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        // 创建购物车
        else
        {
            HttpCookie cookie = new HttpCookie("Cart");
            cookie.Values[product.Id.ToString()] = num.ToString();
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
    }
    #endregion
复制代码
#region 写入cookie
    /// <summary>
    /// 写入cookie
    /// </summary>
    /// <param name="product">商品</param>
    /// <param name="num">个数</param>
    /// <param name="expires">有效期</param>
    public void WriteCookies(Product product, int num = 1, int expires = 15)
    {
        if (System.Web.HttpContext.Current.Request.Cookies["Cart"] != null)
        {
            string cookieValue = System.Web.HttpContext.Current.Request.Cookies["Cart"].Value;
            // 商品不存在
            if (System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()] == null)
            {
                cookieValue = cookieValue + "&" + product.Id + "=" + num.ToString();
            }
            // 商品存在
            else
            {
                int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()].ToString()) + num;
                System.Web.HttpContext.Current.Request.Cookies["Cart"].Values[product.Id.ToString()] = num1.ToString();
                cookieValue = System.Web.HttpContext.Current.Request.Cookies["Cart"].Value;
            }
            HttpCookie cookie = new System.Web.HttpCookie("Cart", cookieValue);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        // 创建购物车
        else
        {
            HttpCookie cookie = new HttpCookie("Cart");
            cookie.Values[product.Id.ToString()] = num.ToString();
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
    }
    #endregion
复制代码

当点击加入购物车以后,会写入Cookie,并跳转到购物车页面

ShoppingCart.aspx.cs文件

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
        this.DataBind();
    }
    protected void Bind()
    {
        Cart cart = new Cart();
        rptCart.DataSource = cart.ReadCookies();
    }
}
复制代码
public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
        this.DataBind();
    }
    protected void Bind()
    {
        Cart cart = new Cart();
        rptCart.DataSource = cart.ReadCookies();
    }
}
复制代码
复制代码
public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
        this.DataBind();
    }
    protected void Bind()
    {
        Cart cart = new Cart();
        rptCart.DataSource = cart.ReadCookies();
    }
}
复制代码

此页面的功能只是简单的从Cookie中读取已有数据,并显示在页面上,由于只是简单侧说,所以没有实现从购物车取消购买功能,只是简单的罗列。

运行页面如下:

3、总结:

本文就是简单的描述一下自己对于购物车实现的想法,如果有不对的,还请各位多多指教,由于自己最近才开始练习使用三层架构,因此这个小例子也使用了三层,这样做确实使代码的层次结构很容易理解了。

我简单介绍一下这三个层,是传统的BLL、DAL、Model三层,DAL用来从数据库读取数据,BLL其实没有什么作用,只是为了成为三层,因为这个小例子太简单,没有什么业务逻辑可以处理(不知道自己这样使用三层会不会太生拉硬套了,反正就是想让自己从小例子开始习惯使用三层吧),Model层定义了一个Product类来表示商品,最后定义了一个Cart类,它只是实现了将数据写入CooKie和从Cookie读去数据的功能。到此这个小例子就完全结束了。

写在后面的话:当我思考了许久,才想出购物车的实现思路,结构最后才发现,组长给我的任务是在企业站中实现购物车,而企业站中实现购物的功能大都是以留言的方式是实现的,所以自己的这个方案也没有用上;不过,我也从中学到了很多,学到了面对问题,应该首先自己去思考如何解决问题。

转载于:https://www.cnblogs.com/Mryang-blog-cn/p/ShopCar.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值