在asp.net

时间:2016-06-14 08:27:11

标签: c# asp.net gridview shopping-cart cart

我正在购买我的购物车。我想知道如何在购物车内的每个项目中添加数量下拉列表列。而且我希望下拉列表的限制是产品库存的数量。希望得到一个简短的解释。谢谢。 :)

Cart.aspx.cs

    protected void theCart()
    {        

        var dt = new DataTable();
        double totalPrice = 0;
        dt.Columns.Add("product_name");
        dt.Columns.Add("product_price");
        dt.Columns.Add("product_stock");

        List<ShoppingCartModel> shoppingList = (List<ShoppingCartModel>)Session["memberCart"];

        if (shoppingList != null)
        {
            foreach (var item in shoppingList)
            {
                dt.Rows.Add(item.ProductName, item.Price, item.ProductStock);

                totalPrice += item.Price;
            }
        }
        lbl_total.Text = totalPrice.ToString();

        MyGridView.DataSource = dt;
        MyGridView.DataBind();
    }

WebShop.aspx.cs

     protected void pro_tbl_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "addtocart")
        {
            List<ShoppingCartModel> shoppingList = (List<ShoppingCartModel>)Session["memberCart"];

            if (shoppingList == null)
            {
                shoppingList = new List<ShoppingCartModel>();
            }

            ShoppingCartModel shoppingModel = new ShoppingCartModel();

            shoppingModel.ProductId = ((HiddenField)e.Item.FindControl("lblProductId")).Value;
            shoppingModel.ProductName = ((Label)e.Item.FindControl("lbl_pname")).Text;
            shoppingModel.ProductDescription = ((Label)e.Item.FindControl("lbl_pdesc")).Text;
            shoppingModel.ProductStock = Convert.ToInt32(((Label)e.Item.FindControl("lbl_pstock")).Text);
            shoppingModel.Price = Convert.ToDouble(((Label)e.Item.FindControl("lbl_price")).Text);

            shoppingList.Add(shoppingModel);
            Session["memberCart"] = shoppingList;

            lbl_result.Text = "Product has been added to the cart";
        }
    }

0 个答案:

没有答案