I've just completed the Try It Out p243 Chapter 6.
However, when I had <pages theme="Monochrome" /> in web.config, the theme shown in the browser was always Monochrome: I could select DarkGrey from the drop down, close the browser. then re-open the browser and it had "remembered" to select DarkGrey via the cookie. But the theme actually used was Monochrome.
Then I tried changing Web.config so that it had <pages theme="DarkGrey" />.
Saved that, and suddenly i could select whichever theme I wanted from the drop down and it would change the theme used on the browser.
Also it "remembered" which theme was last used via the cookie.
So...why does it work when web.config has <pages theme="DarkGrey" /> but not when it has <pages theme="Monochrome" /> ?
I have been comparing my code with what I could download from Wrox, but to no avail. I am confused at the moment!
Richard
PS: The relevant code follows:
Web.config
code
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<pages theme="Monochrome" />
<compilation debug="false" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
/code
Default.aspx
<code>
<%@ Page Title="Welcome to Planet Wrox" Language="
VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="Default.aspx.
vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>Hi there visitor and welcome to Planet Wrox</h1>
<p class="Introduction">
We're glad you're paying a visit to <a href="http://www.PlanetWrox.com">www.PlanetWrox.com</a>, the coolest music community site on the Internet.
</p>
<p class="Introduction">
Feel free to have a <a href="Default.aspx">look around</a>; there are lots of interesting <strong>reviews and concert pictures</strong> to be found here.
</p>
<p>
You can <a href="Login.aspx">log in</a> here</p>
</asp:Content>
</code>
Default.aspx.vb
<code>
Partial Class _Default
Inherits BasePage
End Class
</code>
MasterPages > Frontend.master
<code>
<%@ Master Language="
VB" CodeFile="Frontend.master.
vb" Inherits="MasterPages_Frontend" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<script src="/Scripts/modernizr-2.6.2.
js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="PageWrapper">
<header><a href="/"></a></header>
<nav>Menu Goes Here</nav>
<section id="MainContent">
<asp:ContentPlaceHolder ID="cpMainContent" runat="server">
</asp:ContentPlaceHolder>
</section>
<aside id="Sidebar">
Select a Theme:<br />
<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGray</asp:ListItem>
</asp:DropDownList>
</aside>
<footer>Footer Goes Here</footer>
</div>
</form>
</body>
</html>
</code>
MasterPages > Frontend.master.vb
<code>
Partial Class MasterPages_Frontend
Inherits System.Web.UI.MasterPage
Protected Sub ThemeList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ThemeList.SelectedIndexChanged
Dim preferredTheme As HttpCookie = New HttpCookie("PreferredTheme")
preferredTheme.Expires = DateTime.Now.AddMonths(3)
preferredTheme.Value = ThemeList.SelectedValue
Response.Cookies.Add(preferredTheme)
Response.Redirect(Request.Url.ToString())
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim selectedTheme As String = Page.Theme
Dim preferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
If preferredTheme IsNot Nothing Then
selectedTheme = preferredTheme.Value
End If
If Not String.IsNullOrEmpty(selectedTheme) Then
Dim item As ListItem = ThemeList.Items.FindByValue(selectedTheme)
If item IsNot Nothing Then
item.Selected = True
End If
End If
End If
End Sub
End Class
</code>
App_Code > BasePage.vb
<code>
Imports Microsoft.VisualBasic
Public Class BasePage
Inherits System.Web.UI.Page
Private Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
If String.IsNullOrEmpty(Me.Title) OrElse Me.Title.Equals("Untitled Page", StringComparison.CurrentCultureIgnoreCase) Then
Throw New Exception("Page title cannot be ""Untitled Page"" or an empty string.")
End If
End Sub
Private Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim preferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
If preferredTheme IsNot Nothing Then
Dim folder As String = Server.MapPath("~/App_Themes/" & preferredTheme.Value)
If System.IO.Directory.Exists(folder) Then
Page.Theme = preferredTheme.Value
End If
End If
End Sub
End Class
</code>
App_Themes > DarkGrey > DarkGrey.css
<code>
*
{
/*
Defines the main font used throughout the entire site.
*/
font-family: Verdana, Arial, Sans-Serif;
}
body
{
/*
Clears white space around the body and sets the background color to a dark grey.
*/
margin: 0;
background-color: #565656;
}
a
{
/*
Clears the underline on normal links, and gives them #FE601A (orange) as the text color.
*/
color: #FE601A;
text-decoration: none;
}
a:hover
{
/*
Underlines links when the user hovers the mouse over them.
*/
text-decoration: underline;
}
h1
{
/*
Makes h1 elements smaller than their browser default.
*/
font-size: 20px;
}
header
{
/*
Sets the background of the header to the same color as the right-hand side of the
header image. This way, the header can expand to the right.
The margin at the bottom creates some room between the header and the menu and the main content.
*/
background-color: #E34600;
margin-bottom: 20px;
}
header a
{
/*
The header a is nested in the header. It provides a link to the homepage,
and sets the header image background.
*/
background-image: url(Images/Header.jpg);
background-repeat: no-repeat;
width: 944px;
height: 138px;
display: block;
}
nav
{
/*
Floats the entire menu on the left of the content.
*/
width: 130px;
float: left;
padding: 10px;
padding-top: 0; /* Reset the top-padding. The first menu item (home) already adds some padding to its top to create room between the nav element and the first menu item */
background-color: #F8F4EF;
margin-left: 35px;
margin-right: 20px;
font-size: 0.8em;
}
.FirstLevelMenuItems
{
padding-top: 10px;
}
section#MainContent
{
/*
Defines the main content area. The text color is white (on the gray background set on the body).
The #MainContent section element has a minimum height of 370 pixels, but can grow if necessary.
The font-size is 80% of its parent element, which in this case comes down to 80% of the font
the user has specified as the default font in the browser.
*/
color: white;
font-size: 0.8em;
width: 600px;
min-height: 370px;
padding: 10px;
float: left;
}
aside#Sidebar
{
/*
The Sidebar is positioned absolutely at the right of the page.
It has the same font-size and color as #MainContent
*/
font-size: 0.8em;
width: 124px;
color: White;
float: left;
padding-top: 10px;
}
footer
{
/*
The footer is positioned below all other content.
At the top, it gets a border with a dashed style, while all other sides have no border.
The line-height centers the text in the footer vertically.
*/
text-align: center;
border-top: 2px dashed gray;
height: 37px;
line-height: 37px;
clear: both;
background-color: #FCBB5B;
}
.Introduction
{
font-style: italic;
}
</code>
App_Themes > Monochrome > Monochrome.css
<code>
*
{
/*
Defines the main font used throughout the entire site.
*/
font-family: Arial, Sans-Serif;
}
body
{
/*
Clears white space around the body and gives it a grey background color.
*/
margin: 0;
background-color: #cccccc;
}
a
{
/*
Clears the underline on normal links, and gives them a different text color.
*/
text-decoration: none;
color: #0760B2;
}
a:hover
{
/*
Underlines links when the user hovers the mouse over them.
*/
text-decoration: underline;
}
h1
{
/*
Makes h1 elements smaller than their browser default.
*/
font-size: 20px;
}
h2
{
/*
Gives h2 elements a different background color and size,
and adds a bit of margin at the top to create room between the heading and the preceding element.
*/
font-size: 14px;
color: #7773A1;
margin-top: 10px;
}
#PageWrapper
{
/*
Sets the total width for the entire page. Also sets the background color
which is used to fill the background of the Sidebar in case the MainContent section is taller than the Sidebar.
Uses margin: auto to center the entire page in the middle of the browser's window.
*/
width: 844px;
background-color: #5487c0;
margin: auto;
}
header
{
/*
Gives the header the same width as the PageWrapper. The height creates some room for the logo
that is set with the background-image.
*/
background-image: url(Images/Header.jpg);
width: 844px;
height: 86px;
}
header a
{
/*
The header a is a link nested in header. It provides a link back to the homepage.
The size of the link is the same as the header, so the entire header is clickable.
*/
width: 844px;
height: 86px;
display: block;
}
nav
{
/*
The menu spans the page width, right below the header.
At the top and left a few pixels padding is applied to create some room.
*/
background-image: url(Images/MenuBackground.jpg);
width: 827px;
height: 36px;
padding-top: 7px;
padding-left: 17px;
}
nav a
{
/*
Links in the Menu are white. This gets overriden by styles in chapter 7.
*/
color: #fff;
}
.MainMenu
{
/*
The Menu gets a white border on all four sides.
*/
border: 1px solid #999999;
width: 814px;
height: 19px;
background-color: #555555;
}
.MainMenu ul li
{
/*
Gives the four menu items in the main menu a width of 200px each.
*/
width: 200px;
}
section#MainContent
{
/*
Defines the main content area. The #MainContent element has a minimum height of 500 pixels, but can grow if necessary.
The font-size is 80% of its parent element, which in this case comes down to 80% of the font
the user has specified as the default font in the browser.
*/
font-size: 0.8em;
width: 659px;
border-left: 1px solid white;
border-right: 2px solid white;
float: left;
background-color: #e1e1e1;
min-height: 500px;
padding: 10px;
}
aside#Sidebar
{
/*
The Sidebar is positioned to the right of the MainContent section. It gets the same font-size as the
#MainContent section and gets a background image called Sidebar.jpg.
To ensure the image is visible in (most) browsers on a small page, the element gets a minimum
height of 500px. This is ignored by IE 6.
*/
font-size: 0.8em;
color: White;
background-image: url(Images/Sidebar.jpg);
background-repeat: no-repeat;
background-color: #7773A1;
width: 142px;
min-height: 500px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 20px;
float: left;
}
footer
{
/*
The footer is positioned below all other content (yet still within PageWrapper).
clear: both is used to clear the impact of the float properties used for #MainContent and #Sidebar.
*/
width: 844px;
clear: both;
height: 37px;
background-color: #A8D5FE;
color: White;
text-align: center;
font-size: 0.7em;
font-weight: bold;
line-height: 37px;
}
.Introduction
{
font-style: italic;
color: #3e3e3e;
}
.MyButton
{
color: White;
}
</code>