Accessing a cookie asp.NET
Is there any way to access a cookie in the same page that it is set?
I am setting the value of a cookie in the page load event but when I try to access it in the HTML part of the page it doesnt have the new value yet. How can I get around this?
Thanks.
Re: Accessing a cookie asp.NET
can yo post your code?
I can't think of a reason why it wont be set. Can you, in the form load set the cookie and directly check the cookie value after setting it?
Re: Accessing a cookie asp.NET
Thread moved to ASP.Net forum
Re: Accessing a cookie asp.NET
The code is simple:
'Set 2 cookies 1 for Purchase/Refinance and 1 for Settlement Agent
Response.Cookies("PurcRefi").Value = PurcRefi.Text
Response.Cookies("SettAgent").Value = FirmName.Text
Response.Write("PurcRefi" & Request.Cookies("PurcRefi").Value)
When I write it to the screen it doesnt have the new value until I go to the next page, or refresh this page Any ideas??????
Re: Accessing a cookie asp.NET
The cookies' values are set in the page but they are actually sent to the user's browser in the response headers. The browser creates the cookie and sends the cookies across in each request. So it's not until page execution finishes that you can access the value.
To do this:
vb Code:
Response.Cookies("PurcRefi").Value = PurcRefi.Text
'and then
Response.Write("PurcRefi" & Request.Cookies("PurcRefi").Value)
is pointless, because you have PurcRefi.Text already. Just read the textbox value.
Re: Accessing a cookie asp.NET
The reason why I want to do this is because I want to be able to check the value of this cookie in all of my pages therefore I wanted to put it into an include. I need to display certain things based on that value. so I want the include to work for all pages. Are you telling me that it is not possible to access the value of a cookie on the same page that it was set?
Re: Accessing a cookie asp.NET
Yes, that's what I'm saying, because you already have the value available to you in another variable. If you want to check it across all pages, then in the other pages you would do
Response.Write("PurcRefi" & Request.Cookies("PurcRefi").Value)
to check for the value set by the original page. But on the original page itself, you'd do it via your variable available to you anyways.
The reason as already explained is because the cookie isn't available to the code until the browser sends it back. Adding a cookie in the code is merely an indication or instruction to the browser to do something. It won't write it immediately.