protected void Page_Load(object sender, EventArgs e)
{
try
{
falsename = Session["falsename"].ToString();
// if (Session["falsename"].ToString() =nothing)
// falsename = Session["falsename"].ToString();
if (!IsPostBack)
{
AddProductID = int.Parse(Request["productID"]);
// dtShopProductInfo = BookShopDAL.ShopCartProductIntro(AddProductID).Tables[0];
// Session.Add("ShopCartProductInfo", dtShopProductInfo);
dtShopCart = (DataTable)Session["YourCartTable"];
int pn = dtShopCart.Rows.Count;
int i = 0;
bool hasone = false;
int nowprodID;
string proid;
while (i < pn && !hasone)//这是为了判断商品是否已在购物车中
{
// nowprodID = Int32.Parse(dtShopCart.Rows[i][0].ToString());st
proid = dtShopCart.Rows[i][0].ToString();
nowprodID = Int32.Parse(proid);
if (nowprodID == AddProductID)
{
hasone = true;
}
else
{
i++;
}
}
if (hasone)
{
DataRow oldDR = dtShopCart.Rows[i];
oldDR["商品数量"] = int.Parse(oldDR["商品数量"].ToString()) + 1;
oldDR["小计"] = Int32.Parse(oldDR["商品数量"].ToString()) * Double.Parse(oldDR["单价"].ToString());
}
else
{
// NewRow = new DataRow();
DataRow NewRow = dtShopCart.NewRow();
dtShopProductInfo = BookShopDAL.ShopCartProductIntro(AddProductID).Tables[0];
Session.Add("ShopCartProductInfo", dtShopProductInfo);
NewRow["商品ID"] = AddProductID;
NewRow["商品名称"] = dtShopProductInfo.Rows[0]["商品名称"];
NewRow["单价"] = dtShopProductInfo.Rows[0]["单价"];
NewRow["商品数量"] = 1;
NewRow["小计"] = Convert.ToSingle(dtShopProductInfo.Rows[0]["单价"]);
dtShopCart.Rows.Add(NewRow);//'添加新行到购物车中
}
Session["YourCartTable"] = dtShopCart;
// dgShopCart.DataSource = Session["YourCartTable"];
GridView1.DataSource = Session["YourCartTable"];
// dgShopCart.DataBind();
GridView1.DataBind();
double subt = 0;
pn = dtShopCart.Rows.Count;
for (i = 0; i < pn; i++)
{
subt += Double.Parse(dtShopCart.Rows[i]["小计"].ToString());
}
lbamount.Text ="所有额"+ subt.ToString();
Application["AllTotal"] = lbamount.Text;
}
}
// else
catch
{
Response.Write("<script>alert('请先登陆');</script>");
Response.Redirect("Default.aspx");
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtCheckOut_Click(object sender, EventArgs e)
{
Response.Redirect("CheckOut.aspx");
}
protected void BtContinue_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
/// <summary>
/// RowDeleting
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int count = GridView1.Rows.Count;
DataTable ShopCart = (DataTable)Session["YourCartTable"];
int b = e.RowIndex;
ShopCart.Rows[b].Delete();//可以不做循环,直接 ShopCart.Rows[GridView1.SelectedIndex + 1].Delete();
Session["YourCartTable"] = ShopCart;
try
{
GridView1.DataSource = Session["YourCartTable"];
GridView1.DataBind();
//try
//{
for (int t = 0; t < GridView1.Rows.Count; t++)//从第一一行循环至结束,一次更新所有的成绩
{
GridViewRow gvrow = GridView1.Rows[t];//定义grideviewrow
decimal decLittleTotal = Convert.ToDecimal(ShopCart.Rows[t]["小计"].ToString());
AllTotal += decLittleTotal;
}
if (AllTotal == 0)
{
lbamount.Text = "having clear it";
BtCheckOut.Visible = false;
}
else
{
Application["AllTotal"] = AllTotal;//所有商品的总额
lbamount.Text = "所有额" + Application["AllTotal"].ToString();
}
}
catch
{
Response.Write("false");
}
}
/// <summary>
/// btupdate
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btupdate_Click1(object sender, EventArgs e)
{
DataTable dtShopCart = (DataTable)Session["YourCartTable"];
lbamount.Text = "";
for (int i = 0; i < GridView1.Rows.Count; i++)//从第一一行循环至结束,一次更新所有的成绩
{
GridViewRow gvrow = GridView1.Rows[i];//定义grideviewrow
decimal intQuantity = Convert.ToDecimal(((TextBox)gvrow.Cells[0].FindControl("TxtQuantity")).Text);
decimal decLittleTotal = Convert.ToDecimal(dtShopCart.Rows[i]["单价"]) * intQuantity;//算乘积单个商品的总额
dtShopCart.Rows[i]["商品数量"] = intQuantity;
dtShopCart.Rows[i]["小计"] = decLittleTotal;
AllTotal += decLittleTotal;
}
Session["YourCartTable"] = dtShopCart;
Application["AllTotal"] = AllTotal;//所有商品的总额
lbamount.Text = "所有额" + Application["AllTotal"].ToString();
GridView1.DataSource = Session["YourCartTable"];
GridView1.DataBind();
}
}