购物车代码

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 System.Data.SqlClient;
public partial class show_ShopCart : System.Web.UI.Page
{
    string PID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("Default.aspx");
        }
        if (!IsPostBack)
        {
            if (Request["mode"] == "view")
            {
                ViewCart();
                TotalPrice();
            }
            else
            {
                if (Request["p_id"] != null || Request["p_id"] != "")
                {
                    PID = Request["p_id"].ToString();
                    updateCart();
                    TotalPrice();
                }
            }
        }
    }
    //创建购物车
    public void CreateCart()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("CartTable");
        ds.Tables.Add(dt);

        DataColumn newdc;
        newdc = new DataColumn("p_id",System.Type.GetType("System.Int32"));
        ds.Tables["CartTable"].Columns.Add(newdc);

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

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

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

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

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

        newdc = new DataColumn("Isdelete", System.Type.GetType("System.Int32"));
        newdc.DefaultValue = 0;
        ds.Tables["CartTable"].Columns.Add(newdc);

        Session["mycart"] = dt;
        GVCart.DataSource = ds.Tables["CartTable"].DefaultView;
        GVCart.DataBind();
    }
    //购物车的更新
    public void updateCart()
    {
        if (Session["mycart"] == null)
        {
            //新建一个表
            CreateCart();
            writeCart();
        }
        else
        {
           //如果已有商品,对表进行更新
            writeCart();
        }
    }
    //---------------------------------------------------------------------
    public void writeCart()
    {
        if (Request["mode"] != "view")
        {
            DataTable nowdt = new DataTable("nowCartTable");
            nowdt = (DataTable)Session["mycart"];
            int pn = nowdt.Rows.Count;
            int i = 0;
            bool hasone = false;
            int nowPID;
            //-------------------------判断当前表中是否存有该商品
            while (i<pn && !hasone)
            {
                nowPID = int.Parse(nowdt.Rows[i][0].ToString());
                if (nowPID == Int32.Parse(PID))//判断是否已存放该商品
                {
                    hasone = true;
                }
                else
                {
                    i++;
                }
            }
            if (hasone)
            {
                DataRow olddr = nowdt.Rows[i];
                olddr["num"] = Int32.Parse(olddr["num"].ToString()) + 1;
                olddr["p_total"] = Int32.Parse(olddr["num"].ToString()) * Double.Parse(olddr["p_price"].ToString());
            }
            else
            {
                DataTable dt=SqlHelper.Query("select * from products where p_id="+PID).Tables[0];
                DataRow newdr = nowdt.NewRow();
                newdr[0] = PID;
                newdr[1]=dt.Rows[0]["p_num"].ToString();
                newdr[2] = dt.Rows[0]["p_name"].ToString();
                double price=Double.Parse( dt.Rows[0]["p_price"].ToString());
                //第一次读库,单价和总价一样
                newdr[3] = price;
                newdr[5] = price;
                nowdt.Rows.Add(newdr);
            }
            GVCart.DataSource = nowdt.DefaultView;
            GVCart.DataBind();
            Session["mycart"] = nowdt;
        }
    }
    //查看购物车----------------------------------------------------------
    public void ViewCart()
    {
        if (Session["mycart"] != null)
        {
            DataTable viewdt = (DataTable)Session["mycart"];
            GVCart.DataSource = viewdt.DefaultView;
            GVCart.DataBind();
        }
        else
        {
            this.labcount.Text = "你还没选择商品!";
        }
    }
    //总计--------------------------------------------------------------------------------------------------
    public void TotalPrice()
    {
        if (Session["mycart"] != null)
        {
            int h;
            Double totalprice = 0;
            DataTable dt3 = (DataTable)Session["mycart"];
            if (dt3.Rows.Count > 0)
            {
                for (h = 0; h < dt3.Rows.Count; h++)
                {
                    totalprice += Int32.Parse(dt3.Rows[h][5].ToString());
                }

                this.labcount.Text =totalprice.ToString();
            }
        }
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        int i;
        int m;
        if (Session["mycart"] != null)
        {
            DataTable dtdelete = (DataTable)Session["mycart"];
            for (i = 0; i < this.GVCart.Rows.Count; i++)
            {
                CheckBox chkdelete = (CheckBox)this.GVCart.Rows[i].FindControl("chkdel");
                if (chkdelete.Checked)
                {
                    dtdelete.Rows[i][6] = 1;
                }
            }
            DataRow[] dr = dtdelete.Select("Isdelete>0");
            for (m = 0; m < dr.Length;m++ )
            {
                dr[m].Delete();
            }
            this.GVCart.DataSource = dtdelete.DefaultView;
            this.GVCart.DataBind();
            Session["mycart"] = dtdelete;
            TotalPrice();
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        int i;
        if (Session["mycart"] != null)
        {
            DataTable dtupdate = (DataTable)Session["mycart"];
            for (i = 0; i < this.GVCart.Rows.Count; i++)
            {
                TextBox txb = (TextBox)this.GVCart.Rows[i].FindControl("txbnum");
                dtupdate.Rows[i][4] = Int32.Parse(txb.Text.Trim());
                dtupdate.Rows[i][5] = Int32.Parse(dtupdate.Rows[i][4].ToString()) * Double.Parse(dtupdate.Rows[i][3].ToString());
            }
            this.GVCart.DataSource = dtupdate.DefaultView;
            this.GVCart.DataBind();
            Session["mycart"] = dtupdate;
            TotalPrice();
        }
    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        if (Session["mycart"] == null)
        {
            SqlHelper.MessageShow(this.Page, "你还没有购买商品");
        }
        else
        {
            DataTable dt = (DataTable)Session["mycart"];
            if (dt != null & dt.Rows.Count > 0)
            {
                double Total=Double.Parse(this.labcount.Text.ToString());
                Response.Redirect("dingdan.aspx?Total="+Total);
            }
            else
            {
                SqlHelper.MessageShow(this.Page, "你还没有购买商品");
            }
        }
    }
    protected void btngoon_Click(object sender, EventArgs e)
    {
        if (Session["url"]!=null)
        {
            Response.Redirect(Session["url"].ToString());
          
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值