gpt4 book ai didi

asp.net - 购物车总价问题

转载 作者:行者123 更新时间:2023-12-04 06:10:57 26 4
gpt4 key购买 nike

我正在购物车的最后一页为我的大学项目开发​​一个购物车项目,我想计算购物车中所有产品的总金额,请帮助我解决该代码
对于cart.aspx

<asp:GridView ID="GridView1" 
runat="server"
AutoGenerateColumns="False"
DataKeyNames="id"
DataSourceID="SqlDataSource1"
EmptyDataText="No Item in the Cart">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName"
HeaderText="pName"
SortExpression="pName" />
<asp:BoundField DataField="brand"
HeaderText="brand"
SortExpression="brand" />
<asp:TemplateField HeaderText="img"
SortExpression="img">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("img") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# Bind("img") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity"
HeaderText="quantity"
SortExpression="quantity" />
<asp:BoundField DataField="price"
HeaderText="price"
SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<% $ConnectionStrings:shopingConnectionString1 %>"
DeleteCommand="DELETE FROM [completeCart] WHERE [id] = @id"
InsertCommand="INSERT INTO [completeCart] ([uName], [pName], [brand], [img], [quantity], [price]) VALUES (@uName, @pName, @brand, @img, @quantity, @price)"
SelectCommand="SELECT * FROM [completeCart] WHERE ([uName] = @uName)"
UpdateCommand="UPDATE [completeCart] SET [uName] = @uName, [pName] = @pName, [brand] = @brand, [img] = @img, [quantity] = @quantity, [price] = @price WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
</InsertParameters>
<SelectParameters>
<asp:CookieParameter CookieName="uname"
Name="uName"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
</asp:SqlDataSource>

对于代码隐藏文件
protected void Page_Load(object sender, EventArgs e)
{
string s2 = System.Web.HttpContext.Current.User.Identity.Name;
Response.Cookies["uname"].Value = s2;

}

最后我想要的结果是在同一页面上的标签控件上显示产品的总成本

最佳答案

我在想你的 cart.aspx 页面有一个用于显示购物车项目和价格的网格 View ,那么你可以这样做....

你可以这样做......只需在 GridView.RowDataBound 事件循环 gridview 中找到控件包含价格金额并将它们相加

 decimal grdTotal = 0;

protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));

grdTotal = grdTotal + rowTotal;
}


lbl.Text = grdTotal.ToString("c");

}
}

您可以在价格栏的页脚显示总值(value).......

关于asp.net - 购物车总价问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7781131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com