Solved

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

Posted on 2006-11-28
8
573 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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