⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcart.aspx.cs

📁 shop sales computer, write asp.net code c#
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Mail;
using System.Net;
using System.Drawing;
using System.Data.SqlClient;


public partial class ShopingCart : System.Web.UI.Page
{
  
    GioHang giohang = new GioHang();
    DataTable dt = new DataTable();
    DataSet a = new DataSet();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        bool coroi=false;
        if (!IsPostBack)
        {
            if (Page.ClientQueryString.Length > 4 && Page.ClientQueryString.Substring(5) != "")
            {
                if (Session["cart"] == null)
                {
                    Session["cart"] = giohang;
                    
                }
                else
                {
                    giohang = (GioHang)Session["cart"];
                    
                }
                string id = Request.QueryString.Get("id_sp");
               
                    foreach (LapTop i in giohang.Cart)
                    {
                        if (id == i.ID_SP)
                        { i.SL++; coroi = true; }
                    }
                
                    if (coroi == false)
                    {
                        KetNoiClass kn = new KetNoiClass();
                        kn.MoKetNoi();
                        dt = kn.TruyVan2("select id_sp,ten_sp,gia from sanpham where id_sp=" + id + "");
                        LapTop newlap = new LapTop();
                        newlap.ID_SP = id;
                        newlap.TEN_SP = dt.Rows[0][1].ToString();
                        newlap.GIA = dt.Rows[0][2].ToString();
                        newlap.GIA = newlap.GIA.Substring(0, newlap.GIA.Length - 5);
                        giohang.AddLapTop(newlap);
                        Session["cart"] = giohang;
                        dt = giohang.ToDatatable();
                        kn.DongKetNoi();
                    }
                    else
                    {
                        dt = giohang.ToDatatable();
                    }
                    //Response.Write(dt.Rows[0][3].ToString());        
                    shoping_cart.DataSource = dt;
                    shoping_cart.DataBind();
                }
                else
                {
                    if (Session["cart"] == null)
                    {
                        Session["cart"] = giohang;
                        
                    }
                    else
                    {
                        giohang = (GioHang)Session["cart"];
                        DataTable olddt = giohang.ToDatatable();
                        shoping_cart.DataSource = olddt;
                        shoping_cart.DataBind();
                        
                    }
                }
            
        }     
    }  
   
    protected void cart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //ID lap
            ((Literal)e.Row.FindControl("IDLap")).Text = Convert.ToString(((DataRowView)e.Row.DataItem)["id_sp"]);
            //Ten lap
            ((Literal)e.Row.FindControl("tenLap")).Text = Convert.ToString(((DataRowView)e.Row.DataItem)["ten_sp"]);
            // SL lap
            string soLuong = Convert.ToString(((DataRowView)e.Row.DataItem)["sl"]);
            ((HtmlInputText)e.Row.FindControl("soLuongLap")).Value = soLuong;
            // gia lap                          
            ((Literal)e.Row.FindControl("giaLap")).Text = String.Format("{0:0,0}", Convert.ToDouble(((DataRowView)e.Row.DataItem)["gia"]));

        }
        else 
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DataTable gioHangMau = giohang.ToDatatable();
                double tongtien = 0.00;
                int tonglap=0;
                for (int i = 0; i < gioHangMau.Rows.Count; i++)
                {
                    tonglap += Convert.ToInt32(gioHangMau.Rows[i]["sl"]);
                    tongtien += Convert.ToInt32(gioHangMau.Rows[i]["sl"]) * Convert.ToDouble(gioHangMau.Rows[i]["gia"]);
                }
                ((Literal)e.Row.FindControl("tongSoLap")).Text = Convert.ToString(tonglap);
                ((Literal)e.Row.FindControl("giaTongCong")).Text = String.Format("{0:0,0}",tongtien);
            }
        }
    }
    protected void xoaGioHang_OnServerClick(object sender, EventArgs e)
    {
        Session.Remove("cart");
        
       Response.Redirect("Default.aspx");
       
    }
    protected void capNhatGioHang_OnServerClick(object sender, EventArgs e)
    {
        giohang = (GioHang)Session["cart"];
       // DataTable dtGiohang = giohang.ToDatatable();
        foreach (GridViewRow row in shoping_cart.Rows)
        {
            int index = row.RowIndex;
            string idLap = Convert.ToString(shoping_cart.DataKeys[index].Value);
            LapTop lap = giohang.GetLaptop(idLap) ;
            //MessageBox.Show(giohang.Cart.Count.ToString());
            HtmlInputText slTextBox = (HtmlInputText)row.FindControl("soLuongLap");
            try
            {
              
                int slLap = Convert.ToInt32(slTextBox.Value);
                
                if (slLap <= 0)
                {
                   
                    giohang.RemoveLap(lap);
                    Session["cart"] = giohang;
                }
                else
                {

                    if (slLap != lap.SL)
                    {
                        lap.SL = slLap;
                        giohang.ChangeQuality(lap.ID_SP, lap.SL);
                        Session["cart"] = giohang;
                    }
                }
            }
            catch
            {}
            
        }
        Response.Redirect("ShoppingCart.aspx");
    }
    protected void tiepTucMuaHang_OnServerClick(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void thanhToan_OnServerClick(object sender, EventArgs e)
    {
        giohang=(GioHang)Session["cart"];
        if (giohang.Cart.Count > 0)
        {
            Response.Redirect("NewOrder.aspx");
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -