GridView购物车使用js进行产品数量的增加问题
我用GridView和js做的购物车产品数量的加减 可是当产品个数为一行时 产品的总价的值不变只有小计的值变化
另一个问题是 产品数量使用加号和减号进行改变后 跳转到订单那页时 购物中产品的数量为1 哪位前辈麻烦给看看吧
下面是代码
aspx页 code
<script type="text/javascript"> function jia(ele) { tr = ele.parentNode; while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode; num = tr.cells[5].getElementsByTagName("input")[0]; var t = parseInt(num.value, 10); if (isNaN(t)) num.value = 0; else num.value = t + 1; countRow(tr) } function jian(ele) { tr = ele.parentNode; while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode; num = tr.cells[5].getElementsByTagName("input")[0]; var t = parseInt(num.value, 10); if (isNaN(t)) num.value = 0; else { if (t < 1) return; num.value = t - 1; } countRow(tr) } function bian(ele) { tr = ele.parentNode; while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode; countRow(tr) } function countRow(row) { price = parseFloat(row.cells[3].innerHTML); if (isNaN(price)) { row.cells[6].innerHTML = "0" return; } num = row.cells[5].getElementsByTagName("input")[0]; t = parseInt(num.value, 10); if (isNaN(t)) t = 0; row.cells[6].innerHTML = roundPrice(price * t); CountAll(); } function CountAll() { var total = 0; table = document.getElementById('<%=GridView1.ClientID %>'); if (table.rows.length < 3) return; for (i = 1; i < table.rows.length; i++) { p = parseFloat(table.rows[i].cells[6].innerHTML); if (isNaN(p)) p = 0; total += p; } document.getElementById('<%=TotalPrice.ClientID %>').innerHTML = roundPrice(total); } function roundPrice(x) { return Math.round(x * 100) / 100; } </script><body> <td> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode" Width="1052px" onrowdeleting="GridView1_RowDeleting" CssClass="No" onrowdatabound="GridView1_RowDataBound" > <Columns> <asp:BoundField DataField="ProductCode" HeaderText="商品编号" > </asp:BoundField> <asp:ImageField DataAlternateTextField="ProImage" DataImageUrlField="ProImage" DataImageUrlFormatString="../UploadFile/img/{0}" HeaderText="商品图片"> </asp:ImageField> <asp:BoundField DataField="ProductName" HeaderText="商品名称" /> <asp:TemplateField HeaderText="价格"> <ItemTemplate> <%#Eval("MallPrice") %> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Intergral" HeaderText="赠送积分" /> <asp:TemplateField HeaderText="商品数量"> <ItemTemplate> <a href="#" onclick="jian(this);return false;">-</a> <input type="text" id="num" value='<%#Eval("Num")%>' onchange="bian(this)" /> <a href="#" onclick="jia(this);return false;">+</a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="小计"> <ItemTemplate> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="删除此商品" ShowDeleteButton="True" > <ControlStyle BorderWidth="0px" Width="40px" /> </asp:CommandField> </Columns> </asp:GridView>
public partial class Store_ShopCart : System.Web.UI.Page{ DataTable cart; DataTable cartTable; Farm.BLL.MarketProduct c = new Farm.BLL.MarketProduct(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindShopCart(); } } //绑定购物车 void BindShopCart() { if (Session["Cart"] == null) { this.ImageButton1.Visible = false; this.Label1.Text = "您还没有购买任何产品!"; } else { cartTable = (DataTable)Session["Cart"]; if (cartTable.Rows.Count == 0) { this.Label1.Visible = true; this.Label1.Text = "您的购物车上还没有装东西!"; this.ImageButton1.Visible = false; this.GridView1.Visible = false; //this.TotalPrice.Text = "0.00"; } else { this.Label1.Visible = false; this.ImageButton1.Visible = true; this.GridView1.Visible = true; //创建一个数据表 cart = new DataTable(); DataColumn c1 = new DataColumn("ProductCode"); DataColumn c2 = new DataColumn("ProImage"); DataColumn c3 = new DataColumn("ProductName"); DataColumn c4 = new DataColumn("MallPrice"); DataColumn c5 = new DataColumn("Intergral"); DataColumn c6 = new DataColumn("Num"); //把每行的数据添加到数据表中 cart.Columns.Add(c1); cart.Columns.Add(c2); cart.Columns.Add(c3); cart.Columns.Add(c4); cart.Columns.Add(c5); cart.Columns.Add(c6); //int i = 1;商品在购物车上的序号 int Intergral = 0; //float price;//商品单价 //int num;//商品数量 //float TPrice = 0;//商品总价 //float total = 0;//商品总数 //创建数据行 执行 DataRow row; foreach (DataRow dr in cartTable.Rows) { //获取图片 int id = Convert.ToInt32(dr["ProductCode"].ToString()); string sql = string.Format("select ProImage from MarketProduct where id='{0}'", id); string flag = Farm.DBUtility.DbHelperSQL.GetSingle(sql).ToString(); //创建一个新行 row = cart.NewRow(); row["ProductCode"] = dr["ProductCode"]; row["ProImage"] = flag; row["ProductName"] = dr["ProductName"]; row["MallPrice"] = dr["MallPrice"]; row["Intergral"] = Intergral; row["Num"] = dr["Num"]; //把创建的行添加到表中 cart.Rows.Add(row); } this.GridView1.Visible = true; this.GridView1.DataSource = cart.DefaultView; this.GridView1.DataBind(); TotalPrice.Text = totalCount.ToString(); } } } protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { if (Session["Cart"] == null) { Farm.Common.JScript.MessageBox.Show(this, "不好意思,您的购物车为空!"); return; } else { Session["Cart"] = null; //this.TotalPrice.Text = "0.00"; this.Response.Redirect("index.aspx"); } } protected void ImageButton2_Click(object sender, ImageClickEventArgs e) { Response.Redirect("index.aspx"); } /// <summary> /// 对应的行删除事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { if (Session["Cart"] == null) { this.Response.Redirect("index.aspx"); return; } else { cartTable = (DataTable)Session["Cart"]; cartTable.Rows[e.RowIndex].Delete(); //重新绑定购物车 Session["Cart"] = cartTable; BindShopCart(); } } decimal totalCount = 0; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { String n = DataBinder.Eval(e.Row.DataItem, "Num").ToString(); String price = DataBinder.Eval(e.Row.DataItem, "MallPrice").ToString(); int num = 0; Int32.TryParse(n, out num); decimal MallPrice = Convert.ToDecimal(price); totalCount += num * MallPrice; e.Row.Cells[6].Text = (num * MallPrice).ToString(); } if (e.Row.RowType == DataControlRowType.DataRow) { ((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')"); this.TotalPrice.Text = null; } }