469,950 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Q: Can't Remove Cookie

Hi,

I have a C# ASP.Net page allow users to enter their name and will set those
value as cookie after click on "Submit" button. In addition, if user click
on "Change" button, the cookie should be remove. By replacing the same
cookie name with negative expire date, things just work fine.

Unfortunatelly, if I try to use the Cookies.Clear() or Cookies.Remove()
method, the cookie just doesn't remove. Anyone know why?

void ButtonSubmit_Click(object sender, EventArgs e) {
HttpCookie myCookie = new HttpCookie("aCookie");
myCookie.Values["firstname"] = TextBoxFirstname.Text.Trim();
myCookie.Values["lastname"] = TextBoxLastname.Text.Trim();

DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0,0,3,0);
myCookie = dt + ts;

Response.Cookies.Add(myCookie);

Response.Redirect("same.aspx");
}
}
}

// This will work
void ButtonChange_Click(object sender, EventArgs e) {
HttpCookie myCookie = new HttpCookie("aCookie");
myCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(myCookie);
Response.Redirect("same.aspx");
}

// This will NOT work
void ButtonChange_Click(object sender, EventArgs e) {
Response.Cookies.Clear();
Response.Redirect("same.aspx");
}

Thank you.

--
Soul


Nov 18 '05 #1
0 1016

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Urban Bettag | last post: by
6 posts views Thread by Stephane | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.