Letting the user select a theme
hi,
I was going through the book and cant seem to get the programmatic change of themes to work Pages 222-228 of chapter 6.
Below is the code from my MasterPage.Master file. On stepping through the code I found that the if (preferredTheme != null) and the if (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null) are always null. So it never enters these conditions. The selected theme is being stored in the Cookie(i checked the cookie value in FireFox), but it seems that it cannot be read back. Nothing happens when I select a different theme from the dropdown list. the page just postsback but the theme remains unchanged and is the same as the one specified in the web.config.
Please let me know if any further details are needed.
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null)
{
lstPreferredTheme.Items.FindByValue(selectedTheme) .Selected = true;
}
}
}
protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("Preferred Theme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = lstPreferredTheme.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div id = "PageWrapper">
<div id = "Header"><a class="HeaderLink" href="-/" runat="server">Header goes here</a></div>
<div id = "MenuWrapper">Menu goes here</div>
<div id = "MainContent">
<asp:ContentPlaceHolder ID ="cpMainContent" runat="server"></asp:ContentPlaceHolder>
</div>
<div id="Sidebar">Select your theme<asp:DropDownList ID="lstPreferredTheme"
runat="server" AutoPostBack="True"
onselectedindexchanged="lstPreferredTheme_Selected IndexChanged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
<asp:ListItem>ForMe</asp:ListItem>
</asp:DropDownList>
</div>
<div id= "Footer">Footer goes here</div>
</div>
</form>
</body>
</html>
|