Solved

Cant delete a cookie immediatly during execution PostBack - VB.NET

Posted on 2006-11-28
8
558 Views
Last Modified: 2008-02-01
Hi,

I need to delete/expire a cookie in realtime while processing code. This is for when I empty my shopping cart, I want the cookie gone before the cart (which is ascx web control) processes as part of the normal postback procedure.

Can anyone tell me why this code doesn't immediatly expire the cookie?

        ' Set a cookie
        System.Web.HttpContext.Current.Response.Cookies("GUID").Value = "HelloWorld"

        ' expire cookie
        Dim aCookie As HttpCookie = Request.Cookies("GUID")
        aCookie.Expires = DateTime.Now.AddDays(-1)
        Response.Cookies.Add(aCookie)

        ' see if we can still read the cookie, WE CAN!!! Not what we want!  
         Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value

After the postback event has completed the cookie is expired, but I really need the cookie expired before I finish executing my postback code!

I know I can do something like Response.Redirect(Request.Url.ToString) at the end of all my code to force 'another' refresh of my page to a pre postback state, but this means about 10 trips back to the database etc.
0
Comment
Question by:bhermer
8 Comments
 
LVL 5

Expert Comment

by:prajapati84
ID: 18034686
Just try this one, u will find what u want...

Label1.Text = System.Web.HttpContext.Current.Response.Cookies("GUID").Value

Regards,
Mukesh
0
 
LVL 6

Expert Comment

by:badalpatel
ID: 18036479
just add this line before ur line of code..

Response.Cookies("GUID").Expires = DateTime.Now()
Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18036873
Hmmm, you are getting a cookie from the Request, setting the expiration, and then adding it to the Response, and the Request still has the cookie.  What is supposed to remove it from the Request object?

Bob
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Author Comment

by:bhermer
ID: 18037629
Hey Bob, not sure I know what I am doing at all!!,

essentially I have a cookie which carries a GUID. I have a web component that sits on the page acting as a cart monitor, this will read the cookie (it reads a Session Var first and then reads the cookie if not there) and extracts the cart from the DB. Once the customer has paid, the cookie gets deleted and the session var gets reset. The problem being, due to the nature of web components, after I delete the cookie and before the page has finished executing the cart monitor re-renders, it looks at the cookie, which hasn't expired until the page finishs loading, finds it, resets the bl**dy session variable and shows as having items in the cart!

After the page is rendered, and refreshed, the cookie is gone. it is just during code execution that the cookie doesnt seem to be deleted.

badalpatel, that doesnt work, and I am pretty sure that is the same as expiring it one day in the past?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 18037752
Looking at the cart monitor component, can it check the expiration date on the cookie?

Bob
0
 
LVL 5

Expert Comment

by:prajapati84
ID: 18042930
You need to postback once to see the effect of cookie after its expiration. I think u should store value of cookie in any contral (e.g. textbox) and then at the time of expiration of cookie, make the value of that control as blank.

Regards,
Mukesh
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now