1

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

document.cookie = "cv0_value=1";

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;

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";

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

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.

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?

3
  • At what point in the request cycle are you changing the value? Commented Nov 9, 2012 at 16:00
  • The value is being changed via a call from the Page_Init. The form is a collection of user controls that are being rendered. Commented Nov 9, 2012 at 16:47
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Nov 9, 2012 at 19:46

1 Answer 1

2

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)
}
Sign up to request clarification or add additional context in comments.

10 Comments

Is it this simple? I'll get back to you after I try it. Thanks.
+1. @McArthey, yes it is - use Response for response data... in general you should treat everything in Request as read only (since it came from client and will never go back) and everything in Response as more or less write-only as it is main purpose to send changes to client.
Unfortunately this doesn't appear to work. Thanks for the clarification on Response and Request, though.
I've called your code above from the Page_Load. The cv0_value has previously been set to "1". Reloading the page multiple times does not result in the value changing to "0".
Are you sure your javascript isn't firing again and resetting the value? Can you show more of your code? Because what I showed will work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.