C# Can';t将产品添加到当前会话ShoppingCart ASP.NET
我正在构建一个具有会话状态的购物车。 但我有一个问题,当我将一个产品添加到购物车中时,它看起来像是创建了一个新的会话,而不是添加到当前会话,所以它只显示最后添加的一个产品 以下是我组织网站的方式: 类*应用程序代码/Cart.cs*C# Can';t将产品添加到当前会话ShoppingCart ASP.NET,c#,asp.net,shopping-cart,C#,Asp.net,Shopping Cart,我正在构建一个具有会话状态的购物车。 但我有一个问题,当我将一个产品添加到购物车中时,它看起来像是创建了一个新的会话,而不是添加到当前会话,所以它只显示最后添加的一个产品 以下是我组织网站的方式: 类*应用程序代码/Cart.cs* public class Cart { DataTable cart = new DataTable(); public Cart() { cart.Columns.Add("id"); cart.Columns.Add("image");
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;
}
}
我正在构建一个具有会话状态的购物车。但我有个问题
当我将一个产品添加到购物车中时,它看起来就像创建了一个新的
会话,而不是添加到当前会话,因此只显示最后添加的一个会话
产品
受保护的无效设置CarttoSession(购物车crt)
{
List dt=新列表();
如果(会话[“购物车”]!=null)
{
dt.AddRange((列表)会话[“购物车]);
}
dt.Add(crt);
会话[“购物车”]=dt;
}
公共部分类ProductDetail:System.Web.UI.Page
{
购物车crt=新购物车();
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效datalistProductDetail_ItemCommand(对象源,DataListCommandEventArgs e)
{
开关(例如CommandName)
{
案例“添加”:
DataListItem1=e.项;
int id=int.Parse((item1.FindControl(“idLabel”)作为标签).Text);
字符串名称=(item1.FindControl(“nameLabel”)作为标签);
double price=double.Parse((item1.FindControl(“pricelab”)作为Label.Text);
字符串image=((item1.FindControl(“img”)作为image.ImageUrl.Replace(“~/images/sp/”,”);
crt.addProductToCart(id、图像、名称、价格);
设置车载会话(crt);
打破
}
}
受保护的无效设置CartToSession(购物车crt)
{
List dt=新列表();
如果(会话[“购物车”]!=null)
{
dt.AddRange((列表)会话[“购物车]);
}
dt.Add(crt);
会话[“购物车”]=dt;
}
}
使用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");
它显示为
无法将类型为“GioHang”的对象强制转换为类型为“System.Collections.Generic.List1[GioHang]”的对象`我已经编辑了ProductDetail.aspx的codebehind,正如您在上面所写的那样更改了setCartToSession
。这很好。似乎有一些类型转换错误,您没有正确执行。你能为“GioHang”显示相同的函数吗?“GioHang”在我的语言中只是“Cart:)顺便说一句,刚刚显示了它。修改了我的答案。请查收。
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;
}
}
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");