Hi Everyone

I am busy with a shooping cart but only use cookies to eliminate using a login. I have everything set so that you can add to the basket, but the problem comes in when I want to delete an item from the basket.

I set the specific cookie's expiry date to the day before and it does expire the cookie but only when I close the browser and rerun the code (So with the new session).

How can I expire the cookie immediately? So that when the page posts back the cookie does not show anymore.

Juran

Recommended Answers

All 2 Replies

Member Avatar for siju kuriakose

Hi,
Check this one ...

protected void Page_Load(object sender, EventArgs e)
{ 
       if (!IsPostBack)
        {
          Response.Cookies["yourcookie"].Value = "siju";
        }      

}
void btn_OnClick(object sender, EventArgs e)
{         
    if (Request.Cookies["yourcookie"] != null)
        {
            string x = Request.Cookies["yourcookie"].Value.ToString();
            Response.Write(x);
            // Set its expiration time somewhere in the past               
            Response.Cookies["yourcookie"].Expires = DateTime.Now.AddDays(-1);
         }

}

Using the page_load process to re-check cookie validity should allow it to check whether the 'item' is current or expired each time the page reloads (ie: postback).

By alternating between !IsPostBack and IsPostBack respectively you can customize your page_load to act differently for new page loads vs postback loads as a result of 'form' submissions.

Once a cookie is 'expired' a simple check to see the current state of the cookie should show that. Yes, it may still 'exist' in the browser cache but in an expired state so you just need to determine not only if a cookie exists, but also it's state when checking the cookie and your issue should be solved.

Hope that helps :) Please remember to mark your thread solved once the issue is resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.