您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---

coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---

来源:网络整理     时间:2016/8/25 1:31:12     关键词:coo是什么意思

关于网友提出的“coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---”问题疑问,本网通过在网上对“coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---
描述:

protected void Button1_Click(object sender, EventArgs e)
    {
        this.Response.Cookies["Gogo"].Value = "25";
        string tt = Request.Cookies["Gogo"].Value;
        //如果你先点击了B1的话第一次tt是正确的为25
        //但如果你是先点击了B2再点击B1的话你就会得到10
        //为什么会是10而不是25呢
        //这个就是想不明白
        //下面也是这样;也就是说得到的Cookie值不是刚得到的值而是上一次得到的值
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Response.Cookies["Gogo"].Value = "10";
        string tt = Request.Cookies["Gogo"].Value;
    }
VS2005页面上放两个Button控件然后对cookie进行写读
但出错了;从中午到现在我都把系统重装了但还是这样


解决方案1:

先将原来的cookie删除,然后在重新写入试试!

解决方案2:

你用3个按牛来分别设置和获取。应该不会出这样的问题了。试一下。

解决方案3:

[M]帖子估计都与楼主有关

不好意思
全不会,捡分!
祈祷斑竹不删你的 哈

解决方案4:

^_^

解决方案5:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web"%>


    private string key = "Gogo";
    protected void Page_Render(object sender, EventArgs e) {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        mycookie cook = new mycookie();
        cook.Write(key, "10");
        Response.Write(cook.Read(key));
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        mycookie cook = new mycookie();
        cook.Write(key, "25");
        Response.Write(cook.Read(key));
    }
    
    protected void Button4_Click(object sender, EventArgs e)
    {
        mycookie cook = new mycookie();
        Response.Write(cook.Read(key));
    }
    public class mycookie {
       
       public void Write(string key, string value) {
            HttpContext.Current.Response.Cookies.Add(new HttpCookie(key, value));
            HttpContext.Current.Response.Cookies[key].Expires = DateTime.Today.AddDays(1);
        }
        public string Read(string key) {
            if (HttpContext.Current.Response.Cookies[key]!=null
                && HttpContext.Current.Response.Cookies[key].Value != null)
            {   
                return HttpContext.Current.Response.Cookies[key].Value;
            }
            else if (HttpContext.Current.Request.Cookies[key] != null
                && HttpContext.Current.Request.Cookies[key].Value != null)
            {
                Write(key, HttpContext.Current.Request.Cookies[key].Value);
                return HttpContext.Current.Request.Cookies[key].Value;
            }
            else
            {
                return String.Empty;
            }
        }
    }
    



    Untitled Page


    
    


        
        
        

        
    




解决方案6:

试试它
<%@ Page Language="C#" %>


    
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Response.Cookies["Gogo"].Value = "25";
        ReadCookie("Gogo");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Response.Cookies["Gogo"].Value = "10";
        ReadCookie("Gogo");
    }
    private void ReadCookie(string key){
        if(Response.Cookies["Gogo"]!=null)
            Response.Write(String.Format("Response[\"{0}\"].Value={1}", key, Response.Cookies[key].Value));
        if (Request.Cookies["Gogo"] != null)
             Response.Write(String.Format("Request[\"{0}\"].Value={1}", key, Request.Cookies[key].Value));
    }



    Untitled Page


    
    


        
        

    


看看结果
明白了吗?

解决方案7:

不明白你为什么要这样的功能?
1)Repsonse的cookie是你写进去的,为什么写完就立即(而不是下次访问页面或者postback)读它?你明明知道你写进去的是什么值。
2)Response的cookie更新在你下次访问页面(包括postback)时才会更新到你的Request的Cookie。
3)如果你一定要读你刚写进去的cookie值而等不到下次访问,那你应该将readcookies的代码改为:
string ReadCookies(string name)
{
   return Response.Cookies[name].value
}

解决方案8:

你已经实现了啊。
你设置Response的cookie成功了,但是这些cookie会在返回客户端时返回用户,要到下次用户在提交请求时Request的cookie才会有变化。你设置的是Response的Cookie,检查的是Request的Cookie,这个结果很正常啊。
第一次比较特殊,原因我已经解释过了,和Asp.net自身的实现有关。

解决方案9:

呵呵,你好像没看明白。
你的Response.Cookies设置代码是没问题的。只是你自己对结果的理解有点问题。

解决方案10:

说白了,你写到Response中的cookie值确实要下次请求时才能在下一次的Request中反映出来。你的写法没错,使你的某些想法错了。

解决方案11:

你不要用  string tt = Request.Cookies["Gogo"].Value;而应该在每次设置之前检查上次设置是否成功,如:
string tt = Request.Cookies["Gogo"].Value;
this.Response.Cookies["Gogo"].Value = "10";


以上介绍了“coo是什么意思 |M| 自我感觉不会有错的Cookie操作;却怎么也不能正确读取我的Cookie 难道是我的机子机品不好 ------- 快来救救 ---”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3581019.html

上一篇|M| 如何写一个在线留言提示功能:要求是当有用户登录了系统的时候,这时有另一个用户发送信息给你的时候网页就会提示你有新的信息; 下一篇|M| AjaxPro为什么没办法返回DataSet 我照网上的做为什么怎么也不行

coo是什么意思相关图片

coo是什么意思相关文章