【发布时间】:2018-02-25 22:51:03
【问题描述】:
我目前正在开发一个在线购物车项目。
我希望程序在 GridView 之外的标签中显示 GetTotal()。
<asp:Content ID="Content1" ContentPlaceHolderID="C1" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="832px" >
<Columns>
<asp:TemplateField HeaderText="Product Image">
<ItemTemplate>
<asp:Image ImageUrl='<%# "../" + Eval("Product_Image") %>' runat="server" Height="100px" Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Series">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Product_Series") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Product_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Product_Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Quantity">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
Total Amount:
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<%# GetUnitPrice(Decimal.Parse(Eval("Product_Price").ToString())*Decimal.Parse(Eval("Product_Quantity").ToString())).ToString("N2") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="l6" runat="server" Text=' <%# GetTotal().ToString("N2") %>'></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true"/>
<asp:CommandField ShowDeleteButton="true"/>
</Columns>
</asp:GridView>
GridView 中的所有值都是通过会话从另一个页面传递的,不包括Price 和TotalUnitPrice,它们在下面的脚本中进行计算:
<script runat="server">
decimal TotalUnitPrice;
decimal GetUnitPrice(decimal Price)
{
TotalUnitPrice += Price;
return Price;
}
decimal GetTotal()
{
return TotalUnitPrice;
}
</script>
关于如何做到这一点的任何想法?原因是我希望程序通过标签获取GetTotal()的值,并据此向用户收费。
【问题讨论】:
-
GridView会有很多行,对吧?因此,每个产品都有TotalUnitPrice。在这种情况下,您可以使用<asp:CommandField />,当您选择特定产品时,其TotalUnitPrice将显示在下面的标签中。 -
但是我需要程序根据上面显示的 TotalUnitPrice 向用户收费。也许使用标签不是最好的方法?
-
TotalUnitPrice值已经在GridView中,因此您可以随时在后端代码中访问它。所以我猜你不需要单独的标签来显示价格。要在标签中显示它,无论如何您都必须从 GridView 访问TotalUnitPrice。相反,您可以从 GridView 访问它,然后直接将其发送给用户。