Friday, July 24, 2009

State Management using cookies

// Check if cookie exists, and display it if it does
if (Request.Cookies["lastVisit"] != null)
// Encode the cookie in case the cookie contains client-side script
Label1.Text = Server.HtmlEncode(Request.Cookies["lastVisit"].Value);
else Label1.Text = "No value defined";
// Define the cookie for the next visit
Response.Cookies["lastVisit"].Value = DateTime.Now.ToString();
Response.Cookies["lastVisit"].Expires = DateTime.Now.AddDays(1);

1 comment: