 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

December 15th, 2010, 10:25 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Ch7, p254: CSS Classes not recognized
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"
Any suggestions?
Thanks
|
|

December 16th, 2010, 02:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
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).
Cheers,
Imar
|
|

December 17th, 2010, 05:30 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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;
}
}
|
|

December 17th, 2010, 06:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
If Page.Theme is null, then you haven't:
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.
Cheers,
Imar
|
|

December 20th, 2010, 11:13 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Ch6: Unable to switch themes programmatically
Still having problems with switching of themes.
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.
Web.config is given below:
Code:
<!--<pages theme="DarkGrey" styleSheetTheme="DarkGrey">-->
<pages theme="Monochrome" >
<!--<pages >-->
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
master page
Code:
<%@ Master
Language="C#"
AutoEventWireup="true"
CodeFile="MasterPage.master.cs"
Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<%-- the stylesheet will be used from the theme, and not from this master page as it was done originally --%>
<%-- <link href="../Styles/Styles.css" rel="stylesheet" type="text/css" /> --%>
</head>
<body>
<form id="form1" runat="server">
<div id="PageWrapper">
<div id="Header" ><a class="HeadLink" href="~/" runat="server">Header goes here</a></div>
<div id="MenuWrapper">
<asp:Menu ID="Menu1" runat="server" CssClass="MainMenu"
DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False">
<StaticSelectedStyle CssClass="StaticSelectedStyle" />
<StaticMenuItemStyle CssClass="StaticMenuItemStyle" />
<DynamicHoverStyle CssClass="DynamicHoverStyle" />
<DynamicMenuItemStyle CssClass="DynamicMenuItemStyle" />
<StaticHoverStyle CssClass="StaticHoverStyle" />
</asp:Menu>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
ShowExpandCollapse="False">
</asp:TreeView>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
ShowStartingNode="False" />
</div>
<div id="MainContent">
<asp:ContentPlaceHolder ID="cphMainContent" runat="server">Main Content</asp:ContentPlaceHolder>
</div>
<div id="Sidebar">Select a theme:
<asp:DropDownList ID="lstPreferredTheme" runat="server" AutoPostBack="True"
onselectedindexchanged="lstPreferredTheme_SelectedIndexChanged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
</asp:DropDownList>
</div>
<div id="Footer">© ® Lanical Technlogies</div>
</div>
</form>
</body>
</html>
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 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());
}
}
Thanks for your help.
|
|

December 21st, 2010, 04:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Can you also post the code for the BasePage that sets the theme? Then the code is complete and I can try to reproduce your problem.
Cheers,
Imar
|
|

December 21st, 2010, 02:10 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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");
}
}
}
|
|

December 21st, 2010, 03:06 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Well, that makes it easy; you're missing the part that sets the ID.
Take a look at the exercise on page 226 and further which shows you how to modify the BasePage to apply the theme.
Cheers,
Imar
|
|

December 21st, 2010, 07:12 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Final comments
Thanks Imar, that was a great help.
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.
How would u do that in C#, in the .NET 3.5?
Thanks for all your help again,
mk
|
|
 |