简体   繁体   English

GridView for ASP.NET C#中的购物车

[英]GridView for Shopping Cart in ASP.NET C#

I have made a simple shopping cart, I have completed it but the problem is when someone add an item to the cart it adds, and if that item again added to the cart then it add it as a new product, I need it to update the product if it already exists. 我已经制作了一个简单的购物车,但是已经完成了,但是问题是,当有人将商品添加到购物车中时,如果该商品再次添加到购物车中,然后又将其添加为新产品,我需要对其进行更新产品(如果已经存在)。 any help??? 任何帮助??? this code is written in page load of Cart View Page 此代码写在购物车查看页面的页面加载中

DataTable dtCart = (DataTable)Session["sessiondtCart"];

            GridView1.DataSource = dtCart;
            GridView1.DataBind();    

            int total = 0;

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                total = total + int.Parse(dtCart.Rows[i]["PRODUCT_AMOUNT"].ToString());
            }
            lblTotalAmount.Text = total.ToString();

the code for adding items in the cart 在购物车中添加商品的代码

protected void lnkBtnReadyToServeAdd_Click(object sender, EventArgs e)
        {
            DataTable dtCart = (DataTable)Session["sessiondtCart"];
            dtCart.Rows.Add(lblProductName.Text, lblProductPrice.Text, txtQuantity.Text, imgk1.ImageUrl, int.Parse(lblProductPrice.Text) * int.Parse(txtQuantity.Text));
            Session["sessiondtCart"] = dtCart;

        }

A very basic implementation I would have expected you to be able to come up with. 我希望您能够提出一个非常基本的实现。

protected void lnkBtnReadyToServeAdd_Click(object sender, EventArgs e)
{
    DataTable dtCart = (DataTable)Session["sessiondtCart"];
    foreach(DataRow row in dtCart.Rows)
    {
        if(row["ProductName"] == lblProductName.Text) //check if already exists
        {
            //now we know we need to increment the quantity, because it's already in the cart
            int quantity = row["TotalPrice"] / int.Parse(lblProductPrice.Text);
            row["TotalPrice"] = quantity + int.Parse(txtQuantity.Text);
        }
        else
        {
            //now we know the item wasn't in the cart, so we need to add it
            dtCart.Rows.Add(lblProductName.Text, lblProductPrice.Text, txtQuantity.Text, imgk1.ImageUrl, int.Parse(lblProductPrice.Text) * int.Parse(txtQuantity.Text));
        }
    }

    Session["sessiondtCart"] = dtCart;
}

Quite a few notes here. 这里有一些注意事项。 You don't appear to be using an ID for your products, so instead we're forced to rely on the product name being unique. 您似乎并没有为您的产品使用ID,因此我们被迫依靠唯一的产品名称。 That's not very good, it's usually best to assign each product a unique ID. 这不是很好,通常最好为每个产品分配一个唯一的ID。

You should not store prices in the cart. 您不应将价格存储在购物车中。 You should just store the ProductId and the Quantity. 您应该只存储ProductId和Quantity。 When you want to know the price, you should look up in the database what the price is. 当您想知道价格时,应该在数据库中查找价格是多少。 Otherwise, someone could easily manipulate lblProductPrice to be 0 and get the products for free. 否则,有人可以轻松地将lblProductPrice设置为0并免费获得产品。 It would also simplify the logic for updating the quantity, as you'd no longer have to divide the total price to get the existing quantity. 这也将简化更新数量的逻辑,因为您不必再​​除以总价即可获取现有数量。

And lastly, you're using ints for your prices. 最后,您使用整数作为价格。 That might be fine for a quick demo. 快速演示可能很好。 But what if your product price is $3.99? 但是,如果您的产品价格为3.99美元,该怎么办? You should use a decimal or a double instead of an integer. 您应该使用小数或双精度数而不是整数。 Quantity is probably fine as an int, since most of the time you won't have fractional quantities. 数量可能是整数,因为大多数时候您不会有分数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM