Hello Imar,
I'm a little confused between Server.Transfer and Response.Redirect. I tried to use Server.Transfer instead of Response.Redirect on page 223 just to see what would happen, and it's not what I expected, and I can't come up with an explanation. For example I select "DarkGrey" and I see "DarkGrey in the variable "preferredTheme" and it actually does add the cookie with a value of "DarkGrey" (as I expected).
Code:
protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = lstPreferredTheme.SelectedValue;
Response.Cookies.Add(preferredTheme);
//Response.Redirect(Request.Url.ToString());
Server.Transfer(Request.Url.PathAndQuery);
}
When I get to the code below and after the cookie request, the preferredTheme is still "Monochrome" (Something I did not expect). If I close the website and reopen it it shows "DarkGrey"
Code:
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
// Test to see if theme is still available (maybe it's been discontinued)
string rootPath = Server.MapPath("~/App_Themes");
var theme = System.IO.Directory.GetDirectories(rootPath, preferredTheme.Value);
if (theme.Length > 0)
{
Page.Theme = preferredTheme.Value;
}
}
}
Can you help me understand what is going on?
Thank you.