网站购物车是写入cookie来完成的,在注册用户登陆后购买商品加入购物车时,购物车不能正常显示出来,在用户退出登陆后就购物车中的商品就显示出来了,再进行登陆也能正常显示。正常的是不会区分登陆与否。所以我很纠结,现在暂时设置成无显示商品就自动退出。
页面地址http://ballwest.com/order/Send.aspx?typeUrl=2代码虽然长,重要的地方我都用红色标明了,希望看起来方便点........
购买页面代码部分aspx
<asp:Button ID="Button1" runat="server" Text="加入购物车" CssClass="input_c" OnClick="btnSureDingzhi_Click" OnClientClick="return checkprice();" Height="27"></asp:Button>商品购买页面代码部分aspx.scprotected void btnSureDingzhi_Click(object sender, EventArgs e)
 {
  // //传递的参数是订制的id,参数列表,预览图片的地址,订制数量,订制的总价,使用cookie保存值
  string userID = "0";
  DingzhiOrderModel model = new DingzhiOrderModel();
  model.Userid = 0;
  if (Request.Cookies["bw_user"] != null)
  {
  model.Userid = Convert.ToInt32(Request.Cookies["bw_user"]["user_id"].ToString());
  userID = Request.Cookies["bw_user"]["user_id"].ToString();
  }
  if (Request.QueryString["id"] != null)
  {
  this.ViewState["proid"] = Request.QueryString["id"];
  }  DingzhiOrderManager ordermanager = new DingzhiOrderManager();
  ........... 
  string proid = string.Empty;
  bool isAdd = false;
  // string lbpri = Convert.ToInt32(this.lblprice.Text);
  if (this.hidprice.Value != "0.0000")
  {
  if (Request.Cookies["Order1"] != null && Request.Cookies["Order1"].ToString() != "")
  {
  string cookiesValue = Request.Cookies["Order1"].Value.ToString();
  //解码
  cookiesValue = HttpUtility.UrlDecode(cookiesValue);
  string[] proRecord = cookiesValue.Split('|');
  for (int i = 0; i < proRecord.Length; i++)
  {
  string[] proArr = proRecord[i].Split(',');
  if (proArr.Length == 5)
  {
  proid = proArr[0].ToString();
  if (subProNO == proid)
  {
  isAdd = true;
  break;
  }
  }
  }
  if (isAdd == true)
  {
  Alert.location("../order/Send.aspx?typeUrl=2");
  }
  else
  {
  setCook(subProNO, SLbox, "1", this.ViewState["product_price2"].ToString(), userID, "Order1");//写入cookie
  Alert.location("../order/Send.aspx?typeUrl=2");
  }  }
  else
  {
  setCook(subProNO, SLbox, "1", this.ViewState["product_price2"].ToString(), userID, "Order1");//写入cookie
  Alert.location("../order/Send.aspx?typeUrl=2");
  }
  }
  else
  {
  Alert.alert(this.Page, "暂时不能购买!");
  }
 } void setCook(string subProNO, string num, string isx, string proprice, string userID, string Actions)
 {
  //写入cookie
  string proRecord = subProNO + "," + num + "," + isx + "," + proprice + "," + userID;//此处含有useID
  //可能有中文,进行预编码
  proRecord = HttpUtility.UrlEncode(proRecord);
  Tool.operateBuyCookie(Actions, proRecord, 100);//写入cookie
 }//写入cookie
tool.sc里的operateBuyCookie部分
public static bool operateBuyCookie(string cookieName, string cookieValue, int count)
  {
  HttpCookie myCookie = new HttpCookie(cookieName);
  //cookie 有效期 一个月
  myCookie.Expires.AddMonths(1);
  if (HttpContext.Current.Request.Cookies[cookieName] != null && !HttpContext.Current.Request.Cookies[cookieName].Value.Equals(""))
  {
  myCookie = HttpContext.Current.Request.Cookies[cookieName];
  myCookie.Value = myCookie.Value + "|" + cookieValue;//多段多个商品写入cookie
  }
  else
  {
  myCookie.Value = cookieValue;
  }
  string[] valueArray = myCookie.Value.Split('|');
  ///超过指定个数,把前面的部分清除
................
  HttpContext.Current.Response.Cookies.Add(myCookie);//最后写入cookie
  return true;
  }
购物车页面aspx部分//DataList 控件绑定数据
<asp:DataList ID="dlProList" runat="server" Width="100%" OnItemCommand="dlProList_ItemCommand" OnItemDataBound="dlProList_ItemDataBound">
  <ItemTemplate>
  <img height="100" src='<%# Eval("img")%>' alt="" style="border: solid 1px #dddddd;" />
  <asp:Label runat="server" ID="lbproNamelb" Text='<%# Eval("proName")%>' ForeColor="Red" />
  <%# Eval("proSize")%>
  ¥<span style="color: red;"><%# Eval("proItPrice")%></span><%# (Eval("isCx").ToString() != "3") ? "元" : "积分"%>
  <asp:HiddenField ID="hfproNo" runat="server" Value='<%#Eval("proNum") %>' />
  <asp:HiddenField ID="hfproId" runat="server" Value='<%#Eval("proId")%>' />
  <asp:HiddenField ID="hfCatefist" runat="server" Value='<%#Eval("proType")+"|"+Eval("proCate") %>' />
  <asp:HiddenField ID="hfType" runat="server" Value='<%#Eval("proSize") %>' />
..............
  <asp:LinkButton ID="lbDelete" runat="server" CommandArgument='<%# Eval("index")%>' CommandName="delete">删除</asp:LinkButton>
  </ItemTemplate>
  </asp:DataList>您的购物车<asp:Label ID="Label1" runat="server" Text="" ForeColor="#eeeeee" /><asp:Label ID="Label2" runat="server" Text="" />
//测试用,Label1绑定useID,Label2绑定订单cookie购物车页面aspx.sc部分  actions = 0;
  float amountPrice = 0;
  int amountScore = 0;
  if (Request.Cookies["Order1"] != null && Request.Cookies["Order1"].ToString() != "")
  {//检测cookie读入是否为空,注册用户登录后购买商品进入购物车时显示是空的
   string cookiesValue = Request.Cookies["Order1"].Value.ToString();
   //解码
   cookiesValue = HttpUtility.UrlDecode(cookiesValue);
   Label2.Text = cookiesValue;//读出cookie代码
   string[] proRecord = cookiesValue.Split('|');
   DataTable dt = new DataTable();
//绑定控件
..........
    }else
 {//读出失败cookie则...
   Label2.Text ="<span style='color: #ff0000;'>&nbsp;&nbsp;如果您的商品未加入到购物车请您退出登录后再在左列登录一次即可。</span>";
   Label3.Text = "<b>注意:</b>如果您的商品没有添加到购物车请您退出登录后再在左列登录一次即可。";
   Response.Cookies["bw_user"].Expires = DateTime.Now.AddDays(-1);//自动登出
 
  }
全部代码部分见链接页http://topic.csdn.net/u/20100726/10/c12ff8c7-66bc-4ced-a0ee-76d9ee70240c.html?81074

解决方案 »

  1.   


    走过的 ,没获得cookie我也没办法了
      

  2.   

    读出不cookie?
    先看一下,cookie存到电脑上没有,如果电脑上没有说明存入的地方有错。
    如果电脑上有该cookie文件,那是读的时候出错cookie是在页面加载完时后存到电脑上的,
    如果你在某一行setcookie,到了下一行就读出来的话是读不了的(因为这时cookie还没存入电脑中)
      

  3.   

    购物车不能正常显示出来,在用户退出登陆后就购物车中的商品就显示出来了,再进行登陆也能正常显示。
    显示出来的cookie代码里的userid部分和登陆id是一样的,为什么先没显示出来?我想是跟每台电脑的初始化有关的,仅仅是逻辑分析,具体的分析不清楚,所以才来请教大家
      

  4.   

    web.config的<system.web>里面只有下述节点
    <authentication  mode="None" />
    <httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
    <pages>
    <compilation>
    <httpHandlers>
    <httpModules>
    <globalization>
    <customErrors mode="Off"/>全部查了一次,写入读取cookie完全没问题,只剩web.config配置了