what event to create cookie when leaving a page?

Discussion in 'ASP General' started by Rich, Feb 20, 2004.

  1. Rich

    Rich Guest

    Hello,

    I want to create a cookie that will expire in a few
    mintues after a user leaves my page so that they can
    return without having to login again if they were only
    gone for a few minutes. I tried putting this code in the
    Page_Unload event but got an error message that cookies
    not supported here.
    ....
    Sub Page_Unload 'this not working here
    Dim objCookie As HttpCookie
    objCookie = New HttpCookie("mycookie", "cookie1")
    objCookie.expires = DateTime.Now.AddMinutes(2)
    Response.Cookies.Add( objCookie )
    End Sub
    </script>

    What is the best place to create a cookie that will only
    last for a few mintues after leaving a page

    Thanks,
    Rich
    Rich, Feb 20, 2004
    #1
    1. Advertising

  2. Hi,

    Don't know exactly for ASP.NET but in ASP, cookies must be written
    before anything else HTML coding. With the Page_Unload event, I'm assuming
    that all of your HTML page has been written to the client and made visible
    to him. Obviously, it's way too late to write cookie at this time on the
    server side when the page has been made visible to client side.

    Also, with ASP and probably ASP.NET, the web server give you access to a
    Session state, wich is probably a better choice for doing what you want than
    trying to work out your own time-out cookies.

    S. L.

    "Rich" <> wrote in message
    news:13e9101c3f7fc$7aa74e30$...
    > Hello,
    >
    > I want to create a cookie that will expire in a few
    > mintues after a user leaves my page so that they can
    > return without having to login again if they were only
    > gone for a few minutes. I tried putting this code in the
    > Page_Unload event but got an error message that cookies
    > not supported here.
    > ...
    > Sub Page_Unload 'this not working here
    > Dim objCookie As HttpCookie
    > objCookie = New HttpCookie("mycookie", "cookie1")
    > objCookie.expires = DateTime.Now.AddMinutes(2)
    > Response.Cookies.Add( objCookie )
    > End Sub
    > </script>
    >
    > What is the best place to create a cookie that will only
    > last for a few mintues after leaving a page
    >
    > Thanks,
    > Rich
    Sylvain Lafontaine, Feb 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich
    Replies:
    1
    Views:
    393
    bruce barker
    Feb 20, 2004
  2. brian

    Leaving a Page

    brian, Jun 11, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,546
    Justin Beckwith
    Jun 12, 2004
  3. tshad

    Raise event when leaving Textbox

    tshad, Feb 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,781
    tshad
    Feb 4, 2005
  4. Replies:
    0
    Views:
    158
  5. Zvonko

    Capturing event user leaving page

    Zvonko, Sep 2, 2005, in forum: Javascript
    Replies:
    8
    Views:
    211
    ExGuardianReader
    Sep 10, 2005
Loading...

Share This Page