{
DataTable dt = new DataTable();//创建表
//为表创建列
dt.Columns.Add("cardId", typeof(int));
dt.Columns.Add("cardName", typeof(string));
dt.Columns.Add("cardPrice", typeof(decimal));
dt.Columns.Add("cardNum", typeof(int));
//将列添加行中
DataRow dr = dt.NewRow();
dr["cardId"] = int.Parse(Request.QueryString["cardId"].ToString());
CardType cardType = CardTypeManager.GetCardTypeById(int.Parse(Request.QueryString["cardId"]));
dr["cardName"] = cardType.CardName;
dr["cardNum"] = 1;
dt.Rows.Add(dr);
Session["cart"] = dt;
}
//删除前操作
protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//删除事件处理
DataTable tab = Session["cart"] as DataTable;
//去掉tab中的一行
tab.Rows.RemoveAt(e.RowIndex);
//重新指定数据
Session["cart"] = tab;
//重新绑定数据
dataBindCard();
}
//更新之前的操作
protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新数据,对session中数据进行修改
DataTable tab = Session["cart"] as DataTable;
string input = "";
try
{
//获取文本框的值
input = (gvCart.Rows[e.RowIndex].FindControl("txtUPdate") as TextBox).Text.ToString();
HiddenField hf = gvCart.Rows[e.RowIndex].FindControl("HiddenField1") as HiddenField;
int count = CardManager.GetCardCount(int.Parse(hf.Value));
if (int.Parse(input) > count)
{
gvCart.EditIndex = -1;
dataBindCard();
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('库存不足只有" + count + "张')</script>");
}
else
{
tab.Rows[e.RowIndex]["cardNum"] = int.Parse(input);
Session["cart"] = tab;
//退出编辑模式
gvCart.EditIndex = -1;
//重新绑定数据
dataBindCard();
}
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数量只能为数字')</script>");
return;
}
}
protected void gvCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//退出编辑模式
gvCart.EditIndex = -1;
//重新绑定数据
dataBindCard();
}
protected void gvCart_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCart.EditIndex = e.NewEditIndex;
//重新绑定数据
dataBindCard();
}