Solved

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

Posted on 2006-11-28
8
585 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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