Results 1 to 7 of 7

Thread: Accessing a cookie asp.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    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.

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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?

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Accessing a cookie asp.NET

    Thread moved to ASP.Net forum

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    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??????

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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:
    1. Response.Cookies("PurcRefi").Value = PurcRefi.Text
    2. 'and then
    3. Response.Write("PurcRefi" & Request.Cookies("PurcRefi").Value)

    is pointless, because you have PurcRefi.Text already. Just read the textbox value.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    232

    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?

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width