利用Session实现购物车

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyBookShop.BLL;
using MyBookShop.Models;


public partial class BookDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["bid"] != null)
            {
                initPage(Convert.ToInt32(Request.QueryString["bid"]));
                ViewState["BookId"] = Request.QueryString["bid"].ToString();
            }
        }
    }

    private void initPage(int bid)
    {
        Book book=BookManager.GetBookById(bid);
        book.Clicks++;
        BookManager.ModifyBook(book);
        this.lblAuthor.Text=book.Author;
        this.lblBookName.Text = book.Title;
        this.lblPublisher.Text = book.Publisher.Name;
        this.lblBooksName.Text = book.Category.Name;
        this.lblISBN.Text = book.ISBN;
        this.lblPublishDate.Text = book.PublishDate.ToShortDateString();
        this.lblFonts.Text = book.WordsCount.ToString();
        this.lblPrice.Text = book.UnitPrice.ToString()+"元";
        this.lblContent.Text = book.ContentDescription;
        this.lblAuthorIntroduce.Text = book.AurhorDescription;
        this.lblRecomment.Text = book.EditorComment;
        this.lblCatagory.Text = book.TOC;
        this.imgBook.ImageUrl = StringHandler.CoverUrl(book.ISBN);
    }

    /// <summary>
    /// 购买
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgb_Buy_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["CurrentUser"] != null)//如果用户没有登录
        {
            if (Session["Cart"] == null)//如果购物篮不存在
            {
                this.BuildCart();//创建购物篮并将商品存入
            }
            else//购物篮存在
            {
                DataTable cart = Session["Cart"] as DataTable;
                if (this.ExistBook(cart))//如果购物篮已存在该商品
                {
                    this.BuildSession(cart); //修改购物篮中的商品
                }               
            }
            Response.Redirect("Cart.aspx");//跳转到购物车界面
        }
        else
        {//如果没有登录,跳转到登录界面
            Response.Redirect(@"Membership\UserLogin.aspx");
        }
    }
    /// <summary>
    /// 已有图书
    /// </summary>
    /// <param name="cart"></param>
    /// <returns></returns>
    public bool ExistBook(DataTable cart)
    {
        foreach (DataRow dr in cart.Rows)
        {
            if (dr["BookId"].ToString().Equals(this.lblBookName.Text.Trim()))
            {
                dr["Number"] = Convert.ToInt32(dr["Number"]) + 1;
                Session["Cart"] = cart;
                Response.Redirect("Cart.aspx");
            }
        }
        return true;
    }
    /// <summary>
    /// 新建购物车表
    /// </summary>
    public void BuildCart()
    {//创建购物车
        DataTable cart = new DataTable();//已经创建了表,但是没有字段
        cart.Columns.Add("BookId");
        cart.Columns.Add("BookName");
        cart.Columns.Add("Number");
        cart.Columns.Add("UnitPrice");
        cart.Columns.Add("ImageUrl");//cart表中已有5个字段
      //将点击的商品加入购物车中
        this.BuildSession(cart);//往cart中添加一条记录
    }
    /// <summary>
    /// 添加新书
    /// </summary>
    /// <param name="cart"></param>
    public void BuildSession(DataTable cart)
    {
        //新建一个数据行的记录
        DataRow dr = cart.NewRow();
        dr["BookId"] = "4939";//存商品编号
        dr["BookName"] = this.lblBookName.Text.Trim();//存商品名称
        dr["Number"] = "1";//存商品数量
        dr["UnitPrice"] = this.lblPrice.Text.Substring(0, this.lblPrice.Text.Length - 1); ;//存入单价
        dr["ImageUrl"] = this.lblISBN.Text.Trim();//存入商品图片地址
        cart.Rows.Add(dr);//将数据行加入到cart表中

        Session["Cart"] = cart;//将数据表cart的值存入session保存
    }
}



郑重声明:资讯 【利用Session实现购物车】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——