-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatHang.aspx.cs
157 lines (146 loc) · 4.62 KB
/
DatHang.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class DatHang : System.Web.UI.Page
{
CSDL x = new CSDL();
protected void Page_Load(object sender, EventArgs e)
{
CheckDN();
if (!IsPostBack)
{
if (Request.QueryString["hc"] != null)
{
int Masp = int.Parse(Request.QueryString["hc"]);
DataTable dt = x.GetData("SELECT TenSP,Gia,MauNgoaiThat,PhienBan,HinhSP FROM SANPHAM where IDSanPham=" + Masp);
string Tensp = dt.Rows[0][0].ToString();
string Giasp = dt.Rows[0][1].ToString();
string Maungoaithat = dt.Rows[0][2].ToString();
string Phienban = dt.Rows[0][3].ToString();
string Hinhsp = dt.Rows[0][4].ToString();
ThemVaoGioHang(Tensp, Giasp,Maungoaithat,Phienban, Hinhsp);
}
if (Session["GioHang"] != null)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["GioHang"];
System.String TongTien = "";
foreach (DataRow r in dt.Rows)
{
r["ThanhTien"] = r["Gia"];
TongTien = Convert.ToString(r["ThanhTien"]);
lbtongtien.Text = TongTien.ToString();
}
gvGioHang.DataSource = dt;
gvGioHang.DataBind();
}
if (Session["GioHang"] == null)
{
Response.Write("<script> alert('Giỏ hàng rỗng !')</script>");
}
}
}
public void ThemVaoGioHang(string Tensp, string Giasp,string Maungoaithat,string Phienban, string Hinhsp)
{
DataTable dt;
if (Session["GioHang"] == null)
{
dt = new DataTable();
dt.Columns.Add("TenSP", typeof(string));
dt.Columns.Add("Gia", typeof(string));
dt.Columns.Add("MauNgoaiThat", typeof(string));
dt.Columns.Add("PhienBan", typeof(string));
dt.Columns.Add("HinhSP", typeof(string));
dt.Columns.Add("ThanhTien",typeof(string));
}
else
dt = (DataTable)Session["GioHang"];
DataRow dr = dt.NewRow();
//dr["MaSanPham"] = Masp;
dr["TenSP"] = Tensp;
dr["Gia"] = Giasp;
dr["MauNgoaiThat"] = Maungoaithat;
dr["PhienBan"] = Phienban;
dr["HinhSP"] = Hinhsp;
dr["ThanhTien"] = Giasp ;
dt.Rows.Add(dr);
Session["GioHang"] = dt;
}
public static int SPDaCoTrongGioHang(string Tensp, DataTable dt)
{
try
{
int dong = -1;
for (int i = 0; i < dt.Rows.Count; i++)
{
if ((dt.Rows[i]["TenSP"].ToString()) == Tensp)
{
dong = i;
break;
}
}
return dong;
}
catch (Exception)
{
return -1;
}
}
protected void gvGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Xoa")
{
int chiso = int.Parse(e.CommandArgument.ToString());
try
{
DataTable dt = (DataTable)Session["Giohang"];
dt.Rows.RemoveAt(chiso);
Session["Giohang"] = dt;
Response.Redirect("~/DonHang.aspx");
}
catch (Exception)
{
Response.Redirect("~/DonHang.aspx");
}
}
}
protected void gvGioHang_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void gvGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvGioHang.PageIndex = e.NewPageIndex;
gvGioHang.DataBind();
}
//protected void btTiepTucMua_Click(object sender, EventArgs e)
//{
//}
// Session["Giohang"] = dt;
// Response.Redirect("~/GioHang.aspx");
//}
private void CheckDN()
{
//try
//{
//string TenDN = Session["Tendangnhap"].ToString();
if (Session["Tendangnhap"] == null)
{
btndathang.Visible = false;
lbthongbao.Visible = true;
}
else
{
btndathang.Visible = true;
lbthongbao.Visible = false;
}
}
protected void btDatHang_Click(object sender, EventArgs e)
{
Response.Redirect("~/DonHang.aspx");
}
}