日期:2014-05-20  浏览次数:20475 次

读出Cooike出错
两个按钮,第一个按钮写入Cooike,第二个按钮读出Cooike,如果多一个判断Cooike是否为空,就会出错.如果不加if   (Request.Cookies[ "name "]   ==   null),不出错!

  protected   void   btnIn_Click(object   sender,   EventArgs   e)
        {
                HttpCookie   hc   =   new   HttpCookie( "myCookie ");
                hc.Values[ "Name "]   =   "this   is   the   name ";
                hc.Values[ "Age "]   =   "23 ";
                hc.Values[ "Date "]   =   System.DateTime.Now.ToString();
                Response.Cookies.Add(hc);
                Response.Write( "写入成功! ");
        }
        protected   void   btnOut_Click(object   sender,   EventArgs   e)
        {      

                if   (Request.Cookies[ "name "]   ==   null)
                {
                        Response.Write( "error ");
                }
                else
                {
                        HttpCookie   hcGet   =   Request.Cookies[ "myCookie "];            
                        Response.Write(hcGet.Values[ "Name "]   +   " <br> "   +   hcGet.Values[ "Age "]   +   " <br> "   +   hcGet.Values[ "Date "]);
                }
             
        }

------解决方案--------------------
if (Request.Cookies[ "myCookie "]== null)

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html