首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

读出Cooike失误

2011-12-13 
读出Cooike出错两个按钮,第一个按钮写入Cooike,第二个按钮读出Cooike,如果多一个判断Cooike是否为空,就会

读出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

热点排行