我正在构建一个具有会话状态的购物车。 但我有一个问题,当我将产品添加到购物车时,它看起来像创建一个新会话,而不是添加到当前会话,所以它只显示最后添加的产品。
这就是我组织网站的方式:
Class * App_Code / Cart.cs *
public class Cart
{
DataTable cart = new DataTable();
public Cart()
{
cart.Columns.Add("id");
cart.Columns.Add("image");
cart.Columns.Add("name");
cart.Columns.Add("price");
cart.Columns.Add("quantity");
cart.Columns.Add("total");
}
public DataTable getCart()
{
return cart;
}
public DataTable addProductToCart(int id, string image, string name, double price)
{
if (cart.Rows.Count != 0)
{
foreach (DataRow row in cart.Rows)
{
if (row["id"].ToString().Equals(id.ToString()))
{
row["quantity"] = int.Parse(row["quantity"].ToString()) + 1;
row["total"] = int.Parse(row["total"].ToString()) * price;
return cart;
}
}
}
DataRow newrow = cart.NewRow();
newrow["id"] = id;
newrow["image"] = image;
newrow["name"] = name;
newrow["price"] = price;
newrow["quantity"] = 1;
newrow["total"] = price;
cart.Rows.Add(newrow);
return cart;
}
这是ProductDetail.aspx的代码隐藏:
public partial class ProductDetail : System.Web.UI.Page
{
Cart crt;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void datalistProductDetail_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Add":
DataListItem item1 = e.Item;
int id = int.Parse((item1.FindControl("idLabel") as Label).Text);
string name = (item1.FindControl("nameLabel") as Label).Text;
double price = double.Parse((item1.FindControl("priceLabel") as Label).Text);
string image = ((item1.FindControl("img") as Image).ImageUrl.Replace("~/images/sp/", ""));
crt.addProductToCart(id, image, name, price);
setCartToSession(crt);
break;
}
}
protected void setCartToSession(Cart crt)
{
Session["Cart"] = crt;
}
}
这是ViewCart.aspx:
public partial class ViewCart: System.Web.UI.Page
{
Cart crt;
protected void Page_Load(object sender, EventArgs e)
{
gh = getGioHangFromSession();
refresh_gridviewCart();
}
protected void refresh_gridviewCart()
{
gridviewCart.DataSource = crt.getCart();
gridviewCart.DataKeyNames = new string[] { "id" };
gridviewCart.DataBind();
}
protected Cart getCartFromSession()
{
if (Session["Cart"] == null)
{
return new Cart();
}
else return (Session["Cart"] as Cart);
}
protected void setCartToSession(Cart crt)
{
Session["Cart"] = crt;
}
}
答案 0 :(得分:1)
我正在构建一个具有会话状态的购物车。但我有一个问题 当我将产品添加到购物车时,它看起来就像创造了一个新产品 会话,不添加到当前会话,所以它只显示最后添加的一个 产品
protected void setCartToSession(Cart crt)
{
List<Cart> dt = new List<Cart>();
if (Session["cart"] != null)
{
dt.AddRange((List<Cart>)Session["Cart"]);
}
dt.Add(crt);
Session["Cart"] = dt;
}
答案 1 :(得分:1)
public partial class ProductDetail : System.Web.UI.Page
{
Cart crt = new Cart();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void datalistProductDetail_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "Add":
DataListItem item1 = e.Item;
int id = int.Parse((item1.FindControl("idLabel") as Label).Text);
string name = (item1.FindControl("nameLabel") as Label).Text;
double price = double.Parse((item1.FindControl("priceLabel") as Label).Text);
string image = ((item1.FindControl("img") as Image).ImageUrl.Replace("~/images/sp/", ""));
crt.addProductToCart(id, image, name, price);
setCartToSession(crt);
break;
}
}
protected void setCartToSession(Cart crt)
{
List<Cart> dt = new List<Cart>();
if (Session["cart"] != null)
{
dt.AddRange((List<Cart>)Session["Cart"]);
}
dt.Add(crt);
Session["Cart"] = dt;
}
}
答案 2 :(得分:0)
使用asp.net在购物车中添加购物车产品的最佳方式
DataSet ds = null;
if (Session["sCart"] == null)
{
ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Product_Code", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Product_Name", typeof(System.String)));
dt.Columns.Add(new DataColumn("Qty", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Price", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Total_Price", typeof(System.Int32)));
ds.Tables.Add(dt);
Session["sCart"] = ds;
}
else
{
ds = (DataSet)Session["sCart"];
}
DataRow row = ds.Tables[0].NewRow();
row["Product_Code"] = lblpcode.Text;
row["Product_Name"] = lblpname.Text;
row["Qty"] = TextBox1.Text;
row["Price"] = lblprice.Text;
row["Total_Price"] = Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(lblprice.Text);
ds.Tables[0].Rows.Add(row);
Response.Redirect("mycart.aspx");