void RequestCookies()
{ HttpCookie cookie = Request.Cookies["shop"];
DataTable dt = new DataTable();
dt.Columns.Add("商品编号");
dt.Columns.Add("商品名称");
dt.Columns.Add("商品价格");
dt.Columns.Add("数量");
dt.Columns.Add("小计"); string[] GoodsID = cookie.Values["GoodsID"].Split(','),GoodsName = cookie.Values["GoodsName"].Split(','), Price = cookie.Values["Price"].Split(',');
for (int index = Math.Min(Math.Min(GoodsID.Length, GoodsName.Length), Price.Length) - 1; index >= 0; index--)
{
DataRow dr1 = dt.NewRow();
dr1[0] = GoodsID[index];
dr1[1] = GoodsName[index];
dr1[2] = Price[index];
dt.Rows.Add(dr1);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind(); }效果如下:
商品编号 商品名称 商品价格 数量 小计
1 比格犬 799
4 吉娃娃犬 1299
2 博美犬 899
问题1:数量和小计那我该怎么写?
问题2:如果出现相同商品,数量如何累加,而不重新出现相同数据
谢谢!
{ HttpCookie cookie = Request.Cookies["shop"];
DataTable dt = new DataTable();
dt.Columns.Add("商品编号");
dt.Columns.Add("商品名称");
dt.Columns.Add("商品价格");
dt.Columns.Add("数量");
dt.Columns.Add("小计"); string[] GoodsID = cookie.Values["GoodsID"].Split(','),GoodsName = cookie.Values["GoodsName"].Split(','), Price = cookie.Values["Price"].Split(',');
for (int index = Math.Min(Math.Min(GoodsID.Length, GoodsName.Length), Price.Length) - 1; index >= 0; index--)
{
DataRow dr1 = dt.NewRow();
dr1[0] = GoodsID[index];
dr1[1] = GoodsName[index];
dr1[2] = Price[index];
dt.Rows.Add(dr1);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind(); }效果如下:
商品编号 商品名称 商品价格 数量 小计
1 比格犬 799
4 吉娃娃犬 1299
2 博美犬 899
问题1:数量和小计那我该怎么写?
问题2:如果出现相同商品,数量如何累加,而不重新出现相同数据
谢谢!
建议你写个实体类或者弄个强类型的dataset也行,那样操作方便一点
做购物车要用Profile不能用Cookie,Cookie容易丢失。购物车用Profile才是正道
基于.net的购物车