I've been working through the C# an
VB versions of the Planet Wrox project and things have gone well until the Dynamically Switching Themes section(Ch.6 p.226). I couldn't get either version to switch, although the dropdown list control behaves as it should: i.e., it holds the selected value, either Monochrome or DarkGrey. Either selection leaves the theme as Monochrome.
I figured I would come back to this later and figure it out if it became necessary, and sure enough, it did. In the Navigation chapter(Ch.7 p.259), the site throws an exception on the select statement in the Page Load routine(debug=true):
Line 24: End If
Line 25:
Line 26: Select Case Page.Theme.ToLower()
Line 27: Case "darkgrey"
Line 28: Menu1.Visible = False
Line 26 is pointed to as the offending statement. The error is "Object reference not set to an instance of an object." I should mention that C# and
VB both behave the same way, so whatever I've done wrong, I've at least been consistent in both projects.
I'm using Win7 Home Premium, VS Webdev Express 2010 SP1, and Firefox 4 to display the site.
Code follows(C# only):
App_Code/BasePage.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage:System.Web.UI.Page
{
private void Page_PreRender(object sender, EventArgs e)
{
if(this.Title == "Untitled Page" || string.IsNullOrEmpty(this.Title) )
{
throw new Exception("Page title cannot be \"Untitled Page\" or empty string");
}
}
public void Page_PreInit(object sender, EventArgs e)
{
HttpCookie preferredTheme = Response.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
Page.Theme = preferredTheme.Value;
}
}
public BasePage()
{
this.PreInit += new EventHandler(Page_PreInit);
this.PreRender += new EventHandler(Page_PreRender);
}
}
FrontEnd.Master.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPages_FrontEnd : System.Web.UI.MasterPage
{
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 (!string.IsNullOrEmpty(selectedTheme) && ThemeList.Items.FindByValue(selectedTheme) != null)
{
ThemeList.Items.FindByValue(selectedTheme).Selected = true;
}
}
switch (Page.Theme.ToLower())
{
case "darkgrey":
Menu1.Visible = false;
TreeView1.Visible = true;
break;
default:
Menu1.Visible = true;
TreeView1.Visible = false;
break;
}
}
protected void ThemeList_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = ThemeList.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
}
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>
<compilation debug="true" targetFramework="4.0" />
<pages theme="Monochrome" styleSheetTheme="Monochrome" ></pages>
</system.web>
</configuration>
I've already checked my code against downloaded code from Wrox and didn't catch any relevant differences. I hope this is enough info to point me in the correct direction.My suspicions are that solving the dynamic switch problem will also clear up the server error at the same time.