S
Soul
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.
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.