You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Have been doing good so far -- but following some of the changes of p254 TIO, the following CSS classes are not recognized in the master page:
HeadLink
MainMenu
StaticSelectedStyle
StaticMenuItemStyle
DynamicHoverStyle
DynamicMenuItemStyle
StaticHoverStyle
and the Errors tab indicate:
"Class or CssClass is not defined" for the above classes
and if I try to view the Default page in IE8, I get the following error:
"Object reference not set to an instance of an object"
It's not a problem that VWD can't find the classses. It's just a warning. At run-time the correct style sheet will be linked (coming from the theme) so the browser will know how to deal with them.
An "Object reference not set to an instance of an object" means an error in your code somehow and is not related to missing CSS classes. Can you post the code surrounding the location of the error (the error message might mention the line nuber).
The code is from the master.cs, and the exception occurs as the program is evaluating the expression in the switch line. During debug, with a break on the switch line, when I hover over on Page.Theme, the value displayed is null.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
publicpartialclassMasterPage : System.Web.UI.MasterPage
{
protectedvoid Page_Load(object sender, EventArgs e)
{
//if not post back
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;
}
}
switch (Page.Theme.ToLower())
{
case"darkgrey":
Menu1.Visible = false;
TreeView1.Visible = true;
break;
default:
Menu1.Visible = true;
TreeView1.Visible = false;
break;
}
}
a) Set the theme in the <pages /> element in web.config
and / or
b) set the theme programatically in the BasePage class.
Can you confirm this is the case?
BTW: if you post code, can you please do the following:
a) Paste and copy the code in Notepad before you post it in the editor to remove color coding
b) Wrap the code in code tags (using the toolbar above the editor)
Because of the (stupid) way this forum handles colored code, it becomes pretty hard to read and debug code otherwise.
When web.config specifies monochrome, I get monochrome theme -- and when I set web.config to DarkGrey, that's the theme I get -- but unable to switch from the browser.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
//if not postback
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;
}
}
//execute either way: postbak or not
switch (Page.Theme.ToLower())
{
//change for "darkgrey", if selected
case "darkgrey":
Menu1.Visible = false;
TreeView1.Visible = true;
break;
//otherwise, initialize for "monochrome"
default:
Menu1.Visible = true;
TreeView1.Visible = false;
break;
}
}
protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
{
//declare and create the cookie
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
//set the experation time of the cookie: 3 months
preferredTheme.Expires = DateTime.Now.AddMonths(3);
//write to the cookie: the user selected theme
preferredTheme.Value = lstPreferredTheme.SelectedValue;
//add the cookie to the cookie collection
Response.Cookies.Add(preferredTheme);
//redirect the request to the same page, thereby reset the theme immediately
Response.Redirect(Request.Url.ToString());
}
}
Unable to switch themes programmatically: BasePage.cs
Here is the code for the BasePage.cs:
Code:
using System;
using System.Web;
/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
public BasePage() //BasePage constructor
{
//Event handler to execute when the PreRender event is thrown: specified by programmer
this.PreRender += new EventHandler(Page_PreRender);
}
//PreRender event handler
private void Page_PreRender(object sender, EventArgs e)
{
if (this.Title == "" || this.Title=="Untitled Page")
{
//throw new Exception("Page title cannot be \"Untitled Page\".");
//Note how the user is notified via this exception w/ a user friendly message
throw new Exception("Page title cannot be empty or untitled");
}
}
}
Apparently, I skipped the drill even though I did read the section because it is all colored and underlined.
A couple remaining questions:
1) What should have been my clue that I was missing some code?
(I did use the debugger, though)
2) Because in the past I used to do ASP.NET 2.0 in VB, I did not include the line to set up the connection between the event and its handler -- and suprisingly, it works anyhow.
Code:
public class BasePage : System.Web.UI.Page
{
public BasePage() //BasePage constructor
{
//Manually setting event and its handler: page's PreRender event
//this.PreRender += new EventHandler(Page_PreRender);
//Manually setting event and its handler: page's PreInit event
//this.PreInit += new EventHandler(Page_PreInit);
}
3) I recall that in the past, using VB, in order to set up the skeleton for the event handler, one would select the object on the left drop down list, then select the event on the right -- to get the event handler skeleton -- rather than having to type the whole thing as the TIO suggested.