我想将多个项目添加到购物车,现在只获得一个项目,下一个项目替换为旧项目

时间:2017-11-02 13:35:41

标签: c# asp.net .net c#-4.0 c#-3.0

我正在开发E-Com网站,并尝试在用户点击添加到购物车按钮时使用列表将多个项目添加到购物车。 但事情是我只能添加当前项目并丢失一次。 我正在使用会话来存储,而不是现在存储在数据库中。 所以请帮助我解决这个问题。

//** Here is what i tried**
//Defined in class//
public class ItemCart
{
    public class ItemList
    {
        public List<ItemDet> ListItems { get; set; }
    }

    public class ItemDet
    {

        public string ItemNo { get; set; }
        public string ItemName { get; set; }
        public string Size { get; set; }
        public string Quantity { get; set; }
        public string Price { get; set; }
        public string Amount { get; set; }

    }
}

protected void BtnCart_Click(object sender, EventArgs e)
{
    List<ItemCart.ItemDet> l;
    List<ItemCart.ItemList> commObj = new List<ItemCart.ItemList>();
    int rowIndex = 0;
    string Quantity = Request.Form["quantity"];
    string lblSalePrc = lblSalePrice.Text;
    string Price = lblSalePrc.ToString();
    string p = Price.Remove(0, 9);
    string Amount = Request.Form[txtAmount.UniqueID];
    l = new List<ItemCart.ItemDet>();
    ItemCart.ItemDet item = new ItemCart.ItemDet
    {
        ItemNo = "0",
        ItemName = h3.InnerText,
        Quantity = Quantity,
        Price = p,
        Amount = Amount,
        Size = "xl"
    };
    l.Add(item);
    ItemCart.ItemList Common = new ItemCart.ItemList
    {
        ListItems = l
    };

    commObj.Add(Common);
    rowIndex += 1;
    Session["OdList"] = l;
    Response.Redirect("Cart.aspx");
}
//Cart.aspx
protected void Page_Load(object sender, EventArgs e)
{
    List<ItemCart.ItemDet> list = (List<ItemCart.ItemDet>)Session["OdList"];
    if (!IsPostBack)
    {
        BindGridview(1);
    }
}
protected void BindGridview(int count)
{
    List<ItemCart.ItemDet> list = (List<ItemCart.ItemDet>)Session["OdList"];
    ListtoDataTable lsttodt = new ListtoDataTable();
    DataTable dt2 = lsttodt.ToDataTable(list);
    DataTable dt = new DataTable();
    dt.Columns.Add("ItemNo", typeof(string));
    dt.Columns.Add("ItemName", typeof(string));
    dt.Columns.Add("Quantity", typeof(string));
    dt.Columns.Add("Price", typeof(string));
    dt.Columns.Add("Amount", typeof(string));
    dt.Columns.Add("Size", typeof(string));

    for (int i = 0; i < count; i++)
    {
        DataRow dr = dt.NewRow();
        dr["ItemNo"] = string.Empty;
        dr["ItemName"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Price"] = string.Empty;
        dr["Amount"] = string.Empty;
        dr["Size"] = string.Empty;
        dt.Rows.Add(dr);

    }
    dt = dt2;
    gvDetails.DataSource = dt;
    gvDetails.DataBind();
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        TextBox ItemNo = (TextBox)gvDetails.Rows[i].FindControl("ItemNo");
        TextBox ItemName = (TextBox)gvDetails.Rows[i].FindControl("ItemName");
        TextBox Quantity = (TextBox)gvDetails.Rows[i].FindControl("Quantity");
        TextBox Price = (TextBox)gvDetails.Rows[i].FindControl("Price");
        TextBox Amount = (TextBox)gvDetails.Rows[i].FindControl("Amount");
        TextBox Size = (TextBox)gvDetails.Rows[i].FindControl("Size");
        ItemNo.Text = dt.Rows[i]["ItemNo"].ToString();
        ItemName.Text = dt.Rows[i]["ItemName"].ToString();
        Quantity.Text = dt.Rows[i]["Quantity"].ToString();
        Price.Text = dt.Rows[i]["Price"].ToString();
        Amount.Text = dt.Rows[i]["Amount"].ToString();
        Size.Text = dt.Rows[i]["Size"].ToString();
    }
}

但是下次当我添加值时,前一个被清除了.. 请建议我合适的方式。

0 个答案:

没有答案