- 爱易网页
-
ASP.NET教程
- 通过hashtable和seesion操作购物车解决办法
日期:2014-05-17 浏览次数:20438 次
通过hashtable和seesion操作购物车
我要把我的库存表信息通过下单添加到缓存表里面,现在库存表里面有两个主键卡号 cn_id和 卡号简写 e_reg。
Hashtable hstable;
if (Session["ShopCart"] == null)
{
hstable = new Hashtable();
hstable.Add(cn_id, Depotname); //添加一个项
Session["ShopCart"] = hstable;
}
else
{
//用户已经有购物车
hstable = (Hashtable)Session["ShopCart"];
if (hstable.Contains(cn_id) && hstable.Values.Equals(hstable[cn_id].ToString())) //如果表内有此该仓库下的卡号,不添加项
{
//int count = Convert.ToInt32(hstable[cn_id].ToString());
//hstable[cn_id] = count + 1;
}
else
hstable.Add(cn_id, Depotname); //如果没有此卡号,则新添加一个项
Session["ShopCart"] = hstable;
}
Response.Redirect("shopcart.aspx");
到shopcart.aspx.cs里面添加数据
//取出存入Hashtable
Hashtable hsCart = (Hashtable)Session["ShopCart"];
if (hsCart.Count == 0)
{
TABLE1.Visible = false;
Panel1.Visible = true;
}
else
{
DataTable dtTable = new DataTable();
DataColumn c1 = new DataColumn("No");
DataColumn c2 = new DataColumn("CardId");
DataColumn c3 = new DataColumn("DepotID");
DataColumn c4 = new DataColumn("Depotname");
DataColumn c5 = new DataColumn("Num");
DataColumn c6 = new DataColumn("Price");
DataColumn c7 = new DataColumn("TotalPrice");
dtTable.Columns.Add(c1);
dtTable.Columns.Add(c2);
dtTable.Columns.Add(c3);
dtTable.Columns.Add(c4);
dtTable.Columns.Add(c5);
dtTable.Columns.Add(c6);
dtTable.Columns.Add(c7);
DataRow row;
//对数据表中进行启遍历,循环为第一列赋新值
foreach (object key in hsCart.Keys)
{
row = dtTable.NewRow();
row["CardId"] = key.ToString();
//row["Num"] = hsCart[key].ToString();
row["Depotname"] = hsCart[key].ToString();
dtTable.Rows.Add(row);
}
int i = 1; //序号
decimal price; //商品单价
int count; // 商品数量
decimal totalPrice = 0; //总数量
Model.Web_StockModel model = new Model.Web_StockModel();
foreach (DataRow drRow in dtTable.Rows)
{
string myRow = drRow["CardId"].ToString();