joe_pool_is
Contributor
Can anyone see why my bigCookie, "cookieMonster", is not being set?
Label2.Text is always "bigCookie = null" whether IsPostBack is true or not, but I don't know how I should rewrite the code to make the changes. Am I writing this cookie the wrong way? Am I reading it in the wrong way? Any suggestions???
Code:
public void Page_Load (object sender, System.EventArgs e) {
string strUserId = "jdoe";
string strName = "John Doe";
string strDate = DateTime.Now.ToString();
HttpCookie bigCookie = Request.Cookies["cookieMonster"];
if (IsPostBack) { // Page Loads for first time
Label1.Text = "Is Postback";
} else { // User Clicked a button
Label1.Text = "Is Not Postback";
}
if (bigCookie == null) {
Label2.Text = "bigCookie = null";
bigCookie = new HttpCookie("cookieMonster");
bigCookie.Values.Add("userId", strUserId);
bigCookie.Values.Add("name", strName);
bigCookie.Values.Add("lastVisit", strDate);
bigCookie.Expires = DateTime.Now.AddDays(365);
bigCookie.Path = Server.MapPath("/");
Response.Cookies.Add(bigCookie);
} else
if (bigCookie["userId"].ToString() == "") {
Label2.Text = "bigCookie = (blank)";
bigCookie = new HttpCookie("cookieMonster");
bigCookie.Values.Add("userId", strUserId);
bigCookie.Values.Add("name", strName);
bigCookie.Values.Add("lastVisit", strDate);
bigCookie.Expires = DateTime.Now.AddDays(365);
bigCookie.Path = Server.MapPath("/");
Response.Cookies.Add(bigCookie);
} else {
Label2.Text = "bigCookie value set";
strUserId = bigCookie["userId"].ToString();
strName = bigCookie["name"].ToString();
strDate = bigCookie["lastVisit"].ToString();
}
}