Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 17th, 2011, 08:25 PM
Registered User
 
Join Date: Jun 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Themes in Chapter 6 once more

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.
Reply With Quote
  #2 (permalink)  
Old June 18th, 2011, 05:50 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look at the following code in the PreInit method:

Code:
HttpCookie preferredTheme = Response.Cookies.Get("PreferredTheme");
Yo're trying to read a cookie from the Response, while you should be reading it from the Request like this:

Code:
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
You also need to remove the theme from web.config, or else two themes are applied at the same time.

Hope this solves the problem, and if not, please let me know.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
dhess (June 18th, 2011)
  #3 (permalink)  
Old June 18th, 2011, 07:46 AM
Registered User
 
Join Date: Jun 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That did it. And I did do the exact same thing in the VB version. Must have suffered an attack of code blindness when reviewing the downloaded code. Thanks for your help and now onward!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6 - Applying the User-Selected Themes JohnB BOOK: Beginning ASP.NET 4 : in C# and VB 6 December 8th, 2010 11:55 AM
Chapter 6 Themes flapjack BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 May 1st, 2010 09:39 AM
Chapter 6 dynamically swithing Themes? yantaq BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 9 January 27th, 2009 06:28 PM
Chapter 5 Applying themes to Page Try it Out mcauliff BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 October 3rd, 2007 11:04 AM
Themes bmains ASP.NET 2.0 Basics 2 August 2nd, 2004 08:48 AM



All times are GMT -4. The time now is 11:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.