问题描述
对于以下问题,我将不胜感激:
我有一个出售游戏等的网站,每个游戏的单独页面上都有一个添加到购物车"链接,单击可带您到"confirmamount.aspx",询问他们要购买多少,键入一个数字,然后转到购物车在一个包含产品编号,产品名称,用户要求购买的数量,每单位价格,然后是总价"的gridview中,所有这些我都可以使用,但是我想在我的gridview的页脚中说出所有项目的总帐单价格,到目前为止,我的代码如下,但是页脚仍为空白.
CONFIRMAMOUNT.ASPX.CS
Hi,
I would appreciate some help for my below issue:
I have a site that sells games etc, each game has an add to cart link on its individual page, clicking that brings you to "confirmamount.aspx" which asks how many they wish to purchase, typing a figure then goes to a shopping cart with a gridview containing the product id, product name, quantity user requested to buy, price per unit, and then "total price", all of this i have working but i want in the footer of my gridview to say the totalbill for all items prices, so far i have below code but the footer just remains blank.
CONFIRMAMOUNT.ASPX.CS
public partial class ConfirmAmount : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int ProductID = int.Parse(Request["id"].ToString());
SqlCommand oCMD = new SqlCommand();
SqlConnection oCON = new SqlConnection();
oCON.ConnectionString = ConfigurationManager.ConnectionStrings["ProductsConnectionString"].ConnectionString;
oCON.Open();
oCMD.Connection = oCON;
oCMD.CommandType = CommandType.StoredProcedure;
oCMD.CommandText = "GetProductNameForID";
oCMD.Parameters.Add(new SqlParameter("@ProdID", SqlDbType.Int));
oCMD.Parameters["@ProdID"].Value = ProductID;
SqlDataReader oRDR = oCMD.ExecuteReader();
oRDR.Read();
lblProduct.Text = oRDR.GetString(oRDR.GetOrdinal("title"));
lblPrice.Text = oRDR.GetString(oRDR.GetOrdinal("price"));
oCON.Close();
}
protected void cmdConfirm_Click(object sender, EventArgs e)
{
((ArrayList)Session["aList"]).Add(new CustomerOrder(int.Parse(Request["id"]), lblProduct.Text, int.Parse(txtQuantity.Text), lblPrice.Text));
Server.Transfer("ShoppingCart.aspx");
}
protected void cmdCancel_Click(object sender, EventArgs e)
{
cmdCancel.Attributes.Add("onClick", "javascript:history.back(); return false;");
}
}
CONFIRMAMOUNT.ASPX
CONFIRMAMOUNT.ASPX
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="ConfirmAmount.aspx.cs" Inherits="ConfirmAmount" %>
<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
<div>
<table>
<tr>
<td style="width: 700px">
<h2 align="center">
style="color: #0441ff; font-family: 'Corbel';">Confirm purchase quantity:</h2>
</td>
</tr>
<tr>
<td>
Adding
<asp:Label ID="lblProduct" runat="server" Text="Label">
to your cart at €
<asp:Label ID="lblPrice" runat="server" Text="Label">
</td>
</tr>
<tr>
<td>
Please enter your quantity:
<asp:TextBox ID="txtQuantity" runat="server">
</td>
</tr>
<tr>
<td>
<asp:Button ID="cmdConfirm" runat="server" Text="Confirm"
onclick="cmdConfirm_Click"/>
<asp:Button ID="cmdCancel" runat="server" Text="Cancel"
onclick="cmdCancel_Click" onclientclick="javascript:history.back(); return false" />
</td>
</tr>
</table>
</div>
CUSTOMER.CS
CUSTOMER.CS
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CustomerOrder
/// </summary>
public class CustomerOrder
{
private int iProductID;
private string sProductName;
private int iQuality;
private string iPrice;
public CustomerOrder(int PID, string PN, int Q, string Pri)
{
this.iProductID = PID;
this.sProductName = PN;
this.iQuality = Q;
this.iPrice = Pri;
}
public int ProductID
{
get
{
return iProductID;
}
set
{
iProductID = value;
}
}
public string Productname
{
get
{
return sProductName;
}
set
{
sProductName = value;
}
}
public int Quantity
{
get
{
return iQuality;
}
set
{
iQuality = value;
}
}
public string ProductPrice
{
get
{
return iPrice;
}
set
{
iPrice = value;
}
}
//public CustomerOrder()
//{
// //
// // TODO: Add constructor logic here
// //
//}
}
SHOPPINGCART.ASPX
SHOPPINGCART.ASPX
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="ShoppingCart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
<div>
<table>
<tr>
<td style="width: 500px">
<h2 align="center">
style="color: #0441ff; font-family: 'Corbel';">Shopping Cart:</h2>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grdProducts" runat="server" AutoGenerateColumns="False"
HorizontalAlign="Center" Width="550px" ShowFooter="True" BorderStyle="None" GridLines="None">
<columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name"
ItemStyle-HorizontalAlign="Center" >
<itemstyle horizontalalign="Center"></itemstyle>
<asp:BoundField DataField="ProductID" HeaderText="Product ID"
ItemStyle-HorizontalAlign="Center" >
<itemstyle horizontalalign="Center"></itemstyle>
<asp:BoundField DataField="ProductPrice" HeaderText="Price"
ItemStyle-HorizontalAlign="Center" DataFormatString="€{0:d}" >
<itemstyle horizontalalign="Center"></itemstyle>
<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:Label ID="lblQuantity" runat="server" BorderColor="Black" Text='<%# Eval("quantity")%>'/>
</itemtemplate>
<footertemplate>
<asp:Label ID="lblBuying" runat="server" BorderColor="Black" />
</footertemplate>
<footerstyle borderstyle="Solid" />
<itemstyle horizontalalign="Center" />
<asp:TemplateField HeaderText="Total Price" ItemStyle-HorizontalAlign="Center" FooterStyle-BorderStyle="Solid">
<itemtemplate>
<asp:Label ID="lblQuPrice" runat="server" BorderColor="Black" Text ='<%# "€" + (Decimal.Parse(Eval("ProductPrice").ToString())*Decimal.Parse(Eval("Quantity").ToString())).ToString("N2") %>'>
</itemtemplate>
<footertemplate>
<asp:Label ID="lblBill" runat="server" BorderColor="Black" />
</footertemplate>
<footerstyle borderstyle="Solid"></footerstyle>
<itemstyle horizontalalign="Center"></itemstyle>
</columns>
<editrowstyle horizontalalign="Center" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"
ForeColor="#003296" />
<footerstyle horizontalalign="Right" />
<asp:SqlDataSource ID="ShopCartDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [id], [title], [quantity], [price] FROM [productTable]">
</td>
</tr>
<tr align="center">
<td>
<asp:Button ID="Button1" runat="server" Text="Buy Now"/>
<asp:Button ID="Button2" runat="server" Text="Continue Shopping"
PostBackUrl="~/HomePage.aspx" />
</td>
</tr>
</table>
</div>
SHOPPINGCART.ASPX.CS
SHOPPINGCART.ASPX.CS
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArrayList aList;
aList = (ArrayList)Session["aList"];
grdProducts.DataSource = aList;
grdProducts.DataBind();
}
decimal theBill = 0;
decimal totalStock = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label lblQuPrice = (Label)e.Row.FindControl("lblQuPrice");
Label lblQuantity = (Label)e.Row.FindControl("lblQuantity");
decimal price = Decimal.Parse(lblQuPrice.Text);
decimal amountToBuy = Decimal.Parse(lblQuantity.Text);
theBill += price;
totalStock += amountToBuy;
}
if(e.Row.RowType == DataControlRowType.Footer)
{
Label lblBill = (Label)e.Row.FindControl("lblBill");
Label lblBuying = (Label)e.Row.FindControl("lblBuying");
lblBill.Text = theBill.ToString();
lblBuying.Text = totalStock.ToString();
}
}
}
推荐答案
SHOPPINGCART.ASPX.CS
SHOPPINGCART.ASPX.CS
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArrayList aList;
aList = (ArrayList)Session["aList"];
grdProducts.DataSource = aList;
grdProducts.DataBind();
}
decimal theBill = 0;
decimal totalStock = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label lblQuPrice = (Label)e.Row.FindControl("lblQuPrice");
Label lblQuantity = (Label)e.Row.FindControl("lblQuantity");
decimal price = Decimal.Parse(lblQuPrice.Text);
decimal amountToBuy = Decimal.Parse(lblQuantity.Text);
theBill += price;
totalStock += amountToBuy;
}
if(e.Row.RowType == DataControlRowType.Footer)
{
Label lblBill = (Label)e.Row.FindControl("lblBill");
Label lblBuying = (Label)e.Row.FindControl("lblBuying");
lblBill.Text = theBill.ToString();
lblBuying.Text = totalStock.ToString();
}
}
}
这篇关于在Gridview页脚中显示总价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!