无法改变在JavaScript中设置cookie值? [英] unable to change cookie value set in javascript?

查看:92
本文介绍了无法改变在JavaScript中设置cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< ASP:复选框的OnClick => 设置为执行以下JavaScript函数:

I have an <asp:CheckBox OnClick=""> set to a JavaScript function that performs the following:

document.cookie = "cv0_value=1";

我检查中该值的.Net code-落后于以下方式和所有似乎工作的罚款。

I am checking this value within the .Net code-behind in the following manner and all appears to be working fine.

cv0_value = Request.Cookies["cv0_value"].Value == "0" ? false : true;

问题是,当我尝试重置后的净code值,其背后似乎并不影响该cookie。

The issue is that when I attempt to later reset the value in the .Net code-behind it does not appear to affect the cookie.

HttpContext.Current.Request.Cookies["cv0_value"].Value = "0";

当在$ C $再次检查值c-后面我发现它仍然设置为在JavaScript中设定的初始值。

When checking the value again in the code-behind I find that it is still set to the original value set in the JavaScript.

我也试图重复调用Page_Load方法,检查请求和响应。尽管与设置时的Page_Load再次调用请求包含原始值的更新值响应。

I have also tried to repeatedly call the Page_Load method, checking the Request and Response. Despite setting the Response with the updated value when the Page_Load is called again the Request contains the original value.

由于在下面的评论我认为这个问题可能是由于从静态方法引用,但我发现的这个解决了该问题。既然我已经提到HttpContext.Current.Response它不会出现在我的实例中的问题。

As discussed in the comments below I believed the issue may be due to referencing from a static method but I found that this addresses the issue. Since I am already referring to HttpContext.Current.Response it does not appear to be the problem in my instance.

有人能解释什么可能回事?

Can someone explain what might be going on?

推荐答案

在您设定的值时,一定要保存它。

After you set the value, make sure you save it.

HttpCookie cookie = HttpContext.Current.Request.Cookies["cv0_value"]
if (cookie != null)
{
   cookie.Value ="0";
   HttpContext.Current.Response.Cookies.Add(cookie)
}

这篇关于无法改变在JavaScript中设置cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆