It looks like you haven't set the
Expires property of the cookie. If you don't provide a date to this property, the cookie will only last for the current browser session (it's not saved to disk, and deleted when you close the last browser window).
On Cookie1.aspx, make the cookie last 30 minutes with this code (from the MSDN page I linked to in this post):
Code:
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 30, 0);
HttpCookie MyCookie = new HttpCookie("Background");
MyCookie.Value = MyDropDownList.SelectedItem.Text;
MyCookie.Expires = dt.Add(ts);
Response.Cookies.Add(MyCookie);
This way, even if you close the browser, the cookie will last for 30 minutes (by saving it to disk).
You should also never assume that a cookie exists. That means that on Cookie2,aspx, you should find out if the cookie is null or not. If it isn't, retrieve and use its value, otherwise make up a (default)value, or skip the code entirely:
Code:
string GetBackground()
{
if (Request.Cookies["Background"] != null)
{
return Request.Cookies["Background"].Value;
}
else
{
// Cookie does not exist. Return a default value
return "#ffffff";
}
}
This way, even if the cookie doesn't exist, you won't get an error, but you get the default background color instead.
Does this help?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.