20 个解决方案
#1
我有例子
#2
那麻烦你 能不能给我看看啊 邮箱是wj1625825287@163.com 可以么
#3
你好能让我借鉴借鉴么
#4
哎呀 很着急啊 同志们 帮帮忙吧 我是真的没办法了
#5
你通过XML来进行购物车的实现,然后用ajax来实现无刷新删除!
#6
不是这样 我现在就需要那个商品的增加减少 因为我这个程序是在触摸屏上跑的 所以只能通过加减 不能输入
#7
没有输入啊,也是点击的!
#8
我现在都蒙了 这个问题都两天了 还没解决 xml不太会用
#9
完整的例子
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
System.Random rd = new System.Random(Environment.TickCount); ;
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TableCellCollection tcc = e.Row.Cells;
int columns = tcc.Count;
tcc.Clear();
e.Row.Cells.Add(new TableCell());
e.Row.Cells[0].Attributes.Add("colspan", columns.ToString());
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
}
}
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[4].Text = (num * MallPrice).ToString();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "<b>总计:</b><span id='AllCount'>" + totalCount.ToString() + "</span>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function jia(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[3].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[3].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[2].innerHTML);
if (isNaN(price)) {
row.cells[4].innerHTML = "0"
return;
}
num = row.cells[3].getElementsByTagName("input")[0];
t = parseInt(num.value, 10);
if (isNaN(t)) t = 0;
row.cells[4].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 - 1; i++) {
p = parseFloat(table.rows[i].cells[4].innerHTML);
if (isNaN(p)) p = 0;
total += p;
}
document.getElementById('AllCount').innerHTML = roundPrice(total);
}
function roundPrice(x) {
return Math.round(x * 100) / 100;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
ShowFooter="true" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号">
<HeaderStyle BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="ProductCode" DataNavigateUrlFormatString="Read.aspx?id={0}"
DataTextField="ProductName" HeaderText="商品名称" Target="_parent" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("MallPrice") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="商品数量" ControlStyle-CssClass="No">
<ItemTemplate>
<a href="#" onclick="jian(this);return false;">-</a><input type="text" 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" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
#10
你好 我现在加减实现了 但是我显示的产品总价 是所有的产品 单个产品总价不现实 而且实在GridView外边的一个label中显示的 我该怎么做啊
#11
这是我后台代码
前台
麻烦给看一下
//绑定购物车
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"];
//用户计算产品数
num = int.Parse(dr["Num"].ToString());
price = float.Parse(dr["MallPrice"].ToString());
//把创建的行添加到表中
cart.Rows.Add(row);
TPrice += num * price;
total += num;
//显示标签
this.TotalPrice.Text = TPrice.ToString();
this.GridView1.Visible = true;
this.GridView1.DataSource = cart.DefaultView;
this.GridView1.DataBind();
}
}
}
}
前台
<table>
<tbody>
<tr class="biao">
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductCode" Width="1052px"
onrowdeleting="GridView1_RowDeleting" CssClass="No"
onrowdatabound="GridView1_RowDataBound"
onrowcreated="GridView1_RowCreated">
<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>
<input type="text" style="border:none" id="MallPrice" value='<%#Eval("MallPrice") %>' /></span>
</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()" />
<a href="#" onclick="jia(this);return false;">+</a>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除次商品" ShowDeleteButton="True" >
<ControlStyle BorderWidth="0px" Width="40px" />
</asp:CommandField>
</Columns>
</asp:GridView>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="6">
<asp:Label ID="Label1" runat="server" ></asp:Label>
商品总金额:
<span>¥<asp:Label ID="TotalPrice"
runat="server" ></asp:Label></span>元</td>
</tr>
麻烦给看一下
#12
document.getElementById('<%=Label1.CLientID%>').innerHTML = roundPrice(total);
就是总价
就是总价
#13
你好 现在是增加数量后总价不出来啊 能帮忙在解决解决么 我后台是按我自己的走的 前台是你给的js 但是还是 点击加减 价格不变啊
#14
你先拷贝我的代码先感受下,
onchange="bian()" />
这些照我的改
onchange="bian()" />
这些照我的改
#15
#16
我实在是看不出我哪里错了 数据源给了 js也用的你的 总价那块我也换成你的那种了 但是他就是不出东西啊
#17
完整的代码
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Intergral", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("ProImage", typeof(System.String)));
System.Random rd = new System.Random(Environment.TickCount); ;
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dr[4] = i;
dr[5] = "http://dotnet.aspx.cc/Images/meng.gif";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
TotalPrice.Text = totalCount.ToString();
}
}
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();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("执行SQL DELETE ProductCode=" + GridView1.DataKeys[e.RowIndex].Value.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<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>
</head>
<body>
<form id="form1" runat="server">
<table>
<tbody>
<tr class="biao"><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
Width="1052px" CssClass="No" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号"></asp:BoundField>
<asp:ImageField DataAlternateTextField="ProImage" DataImageUrlField="ProImage" 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>
</td></tr>
</tbody>
<tfoot>
<tr><td colspan="6">
<asp:Label ID="Label1" runat="server"></asp:Label>
商品总金额: <span>¥<asp:Label ID="TotalPrice" runat="server"></asp:Label></span>元</td>
</tr>
</table>
</form>
</body>
</html>
#18
你要去学习
row[ x].cells[ y]. innerHTML
rows. length
这些红色字含义,不要乱写,乱套!!
row[ x].cells[ y]. innerHTML
rows. length
这些红色字含义,不要乱写,乱套!!
#19
嗯嗯 真的十分感谢你啊 谢谢你了 分全给你了 呵呵
#20
你好 又出现两个问题 首先当数据是一行时那个label的值不变 那个小计栏里的值变化 其次是当填写订单时产品的数量不是购物车里产品的数量 麻烦你再给看看呗
#21
#1
我有例子
#2
那麻烦你 能不能给我看看啊 邮箱是wj1625825287@163.com 可以么
#3
你好能让我借鉴借鉴么
#4
哎呀 很着急啊 同志们 帮帮忙吧 我是真的没办法了
#5
你通过XML来进行购物车的实现,然后用ajax来实现无刷新删除!
#6
不是这样 我现在就需要那个商品的增加减少 因为我这个程序是在触摸屏上跑的 所以只能通过加减 不能输入
#7
没有输入啊,也是点击的!
#8
我现在都蒙了 这个问题都两天了 还没解决 xml不太会用
#9
完整的例子
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
System.Random rd = new System.Random(Environment.TickCount); ;
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TableCellCollection tcc = e.Row.Cells;
int columns = tcc.Count;
tcc.Clear();
e.Row.Cells.Add(new TableCell());
e.Row.Cells[0].Attributes.Add("colspan", columns.ToString());
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
}
}
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[4].Text = (num * MallPrice).ToString();
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "<b>总计:</b><span id='AllCount'>" + totalCount.ToString() + "</span>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function jia(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[3].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[3].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[2].innerHTML);
if (isNaN(price)) {
row.cells[4].innerHTML = "0"
return;
}
num = row.cells[3].getElementsByTagName("input")[0];
t = parseInt(num.value, 10);
if (isNaN(t)) t = 0;
row.cells[4].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 - 1; i++) {
p = parseFloat(table.rows[i].cells[4].innerHTML);
if (isNaN(p)) p = 0;
total += p;
}
document.getElementById('AllCount').innerHTML = roundPrice(total);
}
function roundPrice(x) {
return Math.round(x * 100) / 100;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
ShowFooter="true" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号">
<HeaderStyle BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="ProductCode" DataNavigateUrlFormatString="Read.aspx?id={0}"
DataTextField="ProductName" HeaderText="商品名称" Target="_parent" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("MallPrice") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="商品数量" ControlStyle-CssClass="No">
<ItemTemplate>
<a href="#" onclick="jian(this);return false;">-</a><input type="text" 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" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
#10
你好 我现在加减实现了 但是我显示的产品总价 是所有的产品 单个产品总价不现实 而且实在GridView外边的一个label中显示的 我该怎么做啊
#11
这是我后台代码
前台
麻烦给看一下
//绑定购物车
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"];
//用户计算产品数
num = int.Parse(dr["Num"].ToString());
price = float.Parse(dr["MallPrice"].ToString());
//把创建的行添加到表中
cart.Rows.Add(row);
TPrice += num * price;
total += num;
//显示标签
this.TotalPrice.Text = TPrice.ToString();
this.GridView1.Visible = true;
this.GridView1.DataSource = cart.DefaultView;
this.GridView1.DataBind();
}
}
}
}
前台
<table>
<tbody>
<tr class="biao">
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductCode" Width="1052px"
onrowdeleting="GridView1_RowDeleting" CssClass="No"
onrowdatabound="GridView1_RowDataBound"
onrowcreated="GridView1_RowCreated">
<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>
<input type="text" style="border:none" id="MallPrice" value='<%#Eval("MallPrice") %>' /></span>
</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()" />
<a href="#" onclick="jia(this);return false;">+</a>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除次商品" ShowDeleteButton="True" >
<ControlStyle BorderWidth="0px" Width="40px" />
</asp:CommandField>
</Columns>
</asp:GridView>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="6">
<asp:Label ID="Label1" runat="server" ></asp:Label>
商品总金额:
<span>¥<asp:Label ID="TotalPrice"
runat="server" ></asp:Label></span>元</td>
</tr>
麻烦给看一下
#12
document.getElementById('<%=Label1.CLientID%>').innerHTML = roundPrice(total);
就是总价
就是总价
#13
你好 现在是增加数量后总价不出来啊 能帮忙在解决解决么 我后台是按我自己的走的 前台是你给的js 但是还是 点击加减 价格不变啊
#14
你先拷贝我的代码先感受下,
onchange="bian()" />
这些照我的改
onchange="bian()" />
这些照我的改
#15
#16
我实在是看不出我哪里错了 数据源给了 js也用的你的 总价那块我也换成你的那种了 但是他就是不出东西啊
#17
完整的代码
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Intergral", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("ProImage", typeof(System.String)));
System.Random rd = new System.Random(Environment.TickCount); ;
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dr[4] = i;
dr[5] = "http://dotnet.aspx.cc/Images/meng.gif";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
TotalPrice.Text = totalCount.ToString();
}
}
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();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("执行SQL DELETE ProductCode=" + GridView1.DataKeys[e.RowIndex].Value.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<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>
</head>
<body>
<form id="form1" runat="server">
<table>
<tbody>
<tr class="biao"><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
Width="1052px" CssClass="No" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号"></asp:BoundField>
<asp:ImageField DataAlternateTextField="ProImage" DataImageUrlField="ProImage" 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>
</td></tr>
</tbody>
<tfoot>
<tr><td colspan="6">
<asp:Label ID="Label1" runat="server"></asp:Label>
商品总金额: <span>¥<asp:Label ID="TotalPrice" runat="server"></asp:Label></span>元</td>
</tr>
</table>
</form>
</body>
</html>
#18
你要去学习
row[ x].cells[ y]. innerHTML
rows. length
这些红色字含义,不要乱写,乱套!!
row[ x].cells[ y]. innerHTML
rows. length
这些红色字含义,不要乱写,乱套!!
#19
嗯嗯 真的十分感谢你啊 谢谢你了 分全给你了 呵呵
#20
你好 又出现两个问题 首先当数据是一行时那个label的值不变 那个小计栏里的值变化 其次是当填写订单时产品的数量不是购物车里产品的数量 麻烦你再给看看呗