cookie value doesn't immediately reflect change (vb.net/javascript

G

Guest

I posted a question earlier about communicating between a javascript function
and vb.net. In the end I decided to try using a cookie. I use the
following javascript function (from vb.net) to set the cookie:

sb.Append("<script language='javascript'>")
sb.Append("{if (confirm('Are you sure? ') == true)")
sb.Append("{var date = new Date();
date.setTime(date.getTime()+(60*60*1000)); var expires = '
expires='+date.toGMTString(); document.cookie = 'confirmcookie=1;' + expires
+ '; path=/'}")
sb.Append("else {var date = new Date();
date.setTime(date.getTime()+(60*60*1000)); var expires = '
expires='+date.toGMTString(); document.cookie = 'confirmcookie=0;' + expires
+ '; path=/'}}")
sb.Append("</script>")
Response.Write(sb.ToString())
Response.Flush()


I then try to access that value with the following code:

If Not Request.Cookies("confirmcookie") Is Nothing Then
Response.Write(Request.Cookies("confirmcookie").Value.ToString)
end if


Here's the deal. These two snippets of code are inside a server-side event
of mine. The first time they are called, the cookie appears to be set, but
the value isn't there. The second time they are called, I think the cookie
is still being set, but the value that shows up is from the last time
the cookie was set. It continues on that way, every time returning the value
from the previous time the cookie was set and not the most recent. I even
tried to somehow delete the cookie so that wouldn't happen, but it didn't
work. I tried both a javascript based version:

Response.Write("<script language='javascript'>var expires = ' expires=Thu,
01-Jan-70 00:00:01 GMT; document.cookie = 'confirmcookie=null;' + expires +
'; path=/'</script>")
Response.Flush()

and vb.net version:
Response.Cookies("confirmcookie").Value = ""
Response.Cookies("confirmcookie").Expires = DateTime.Now.AddYears(-50)

but neither helped.

Does anyone have any idea what I'm doing wrong?

Thanks for the help,
JT
 
Joined
Jun 29, 2011
Messages
1
Reaction score
0
I have just encountered this problem. And have found out the cased luckily. If I am not mistaken, you tried to call the Response.Cookies right after the Response.Flush method. If this so, this will not work at all. Just remove the Response.Flush first, and call it again after the Response.Cookies() method has done its work. If you are wonder why... google this for more info. But believe me that, your issue will work out if you do as I suggested. Have a luck !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top