购物车总价格问题

时间:2011-10-15 22:39:29

标签: asp.net datagridview

我正在为我的大学项目的购物车项目工作在我的购物车的最后一页我想计算购物车中所有产品的总金额帮助我在那个代码中,这是 对于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;

}

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

3 个答案:

答案 0 :(得分:2)

删除SqlDataSource控件。不要把SQL放在aspx页面中!创建一个返回购物车并计算总计的类。代码背后的数据绑定。

答案 1 :(得分:1)

我想你的cart.aspx页面有一个用于显示购物车商品和价格的网格视图,那么你可以这样做....

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

 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");

    }
 }

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

答案 2 :(得分:1)

请访问此链接,这实际上就是您要搜索的内容:

http://www.ezzylearning.com/tutorial.aspx?tid=1676759

希望这有帮助。