会话传递到类文件并通过gridview检索

时间:2013-11-24 03:41:42

标签: c# asp.net class datagridview shopping-cart

我将创建一个会话并存储在类文件中,当我点击一个按钮添加到购物车。之后使用gridview进行显示。任何人都可以给我一个简单的概念。我差点放弃做这个因为我不知道。我要加入数据库lolz。当我点击一个按钮添加到购物车代码是否正确?因为我是asp.net的新人。如何在gridview上显示?

我的班级文件出错了

 /*missiong partial modifier on declaration of type'E-commerce_shoppingCart'; 
   another partial declaration of this type exists
public class ShoppingCart
{
    //Consists of a Price (the sum of all of your items)
    public decimal Total { get { return Items.Sum(i => i.Subtotal); } }
    //The total number of items in your cart
    public int TotalItems { get { return Items.Count; } }

    //A Collection of "Cart Items"
    public List<CartItem> Items { get; set; }

    //Basic constructor
    public ShoppingCart()
    {
        Items = new List<CartItem>();
    }
}

//A Shopping Cart Item
public class CartItem
{
    public string ItemID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }

    //Returns the subtotal (price * quantity)
    public decimal Subtotal { get { return Price * Quantity; } }

    public CartItem()
    {

    }

    //Constructor that accepts your items
    public CartItem(string id, string name, decimal price, int quantity)
    {
        ItemID = id;
        Name = name;
        Price = price;
        Quantity = quantity;
    }
}

在我的产品详情页面上我应该如何创建一个会话,并逐个存储,因为有一些契约

if (Session["Cart"] == null)
                {
                    Session["Cart"] = new ShoppingCart();
                }


                ShoppingCart cart = Session["Cart"] as ShoppingCart;


   //below correct? need to put else? how to store one by one           
 cart.Items.Add(new CartItem(Id, lblName.Text, Decimal.Parse(lblPrice.Text), int.Parse(txtAddtoCart.Text)));
//how should i store at class file, my id is passing from query string, assuming now is static


                Session["Cart"] = cart;

1 个答案:

答案 0 :(得分:0)

根据您获得的错误判断,您需要将lblPrice.Text转换为十进制并将txtAddtoCart.Text转换为int。你的方法期望一个小数和一个int,但你现在正在传递所有字符串。

Convert.ToDecimal(lblPrice.Text)
Convert.ToInt32(lbltxtAddtoCart.Text)