Wrox Programmer Forums
|
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 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
 
Old October 16th, 2010, 08:01 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default Ch6 - User Theme Selection not working

The user theme selection in the drop down box does not change the theme.

The name in the drop down box changes from Monochrome to DarkGrey but the displayed theme stays in monochrome.

The postback function seems to work.

I have carefully checked the code but can't see any mistakes.
There are no errroir messages (at least as far as I can see)

What might I have done wrong?
What tests can I run to find the problem?

Thanks
Ken
 
Old October 16th, 2010, 09:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ken,

Can you post the code for the following files:

1- Master page markup
2. Master page Code Behind
3. BaseClass.cs / BaseClass.vb
4. Code Behind of the page you're testing the theme drop down on?

Please use the Remove Text Formatting button (or paste the code in Notepad first) and then wrap the code in code tags (using the # button on the post editor's toolbar) to make sure the code ends up correctly.

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!
 
Old October 16th, 2010, 09:42 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Hi Imar,
Great book!
Here is the code.
Ken


1- Master page markup

Code:
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>
   
</head>
<body>
    <form id="form1" runat="server">
    <div id="PageWrapper">
      <div id ="Header"><a href="~/" runat="server">Header goes here</a> </div>
      <div id ="MenuWrapper">Menu goes here</div>
      <div id ="MainContent"> <asp:ContentPlaceHolder ID="cpMainContent" 

runat="server">This is default text that shows up in content pages that don't 

explicitly override it.</asp:ContentPlaceHolder></div>
      <div id ="SideBar">Select a Theme <br /><asp:DropDownList ID="ThemeList" 

runat="server" AutoPostBack="True">
        <asp:ListItem>Monochrome</asp:ListItem>
        <asp:ListItem>DarkGrey</asp:ListItem>
        </asp:DropDownList>
      </div>
      <div id ="Footer">Footer goes here</div> 
    </div>

    </form>
</body>
</html>
2. Master page Code Behind

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;
        }
      }
    }
    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());
    }
}
3. BaseClass.cs / BaseClass.vb


Code:
using System;
using System.Web;

public class BasePage : System.Web.UI.Page
{

  private void Page_PreInit(object sender, EventArgs e)
  {
    HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
    if (preferredTheme != null)
    {
      Page.Theme = preferredTheme.Value;
    }
  }  
  
  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 an empty 

string.");
    }
  }

  
  
  public BasePage()
	{
      this.PreRender += new EventHandler(Page_PreRender);
      this.PreInit +=new EventHandler(Page_PreInit);
	}
}

Code behind of Default.aspx

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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
The Following User Says Thank You to ken evans For This Useful Post:
yavapai (April 27th, 2011)
 
Old October 16th, 2010, 09:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

From the code behind of Default.aspx:

Quote:
public partial class _Default : System.Web.UI.Page
There you have it: the page is not inheriting BasePage and thus the theme is never applied. Change System.Web.UI.Page to BasePage and it should work; you'll find instructions on how to do this globally for the whole site in the book.

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!
 
Old October 16th, 2010, 10:04 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Hmmm,
Yes - I suspected that I wrecked the BasePage references in the site when I used the "replace all" feature in an earlier exercise before reading the rest of the text which includes a warning.
Then I tried to figure out where the 9 replacements were but I became a bit lost.

The bad news is that I have now changed the code as you advised (see below) but the DarkGrey theme still does not appear. The malfunction is the same as I described in my initial post.

???
Thanks
Ken

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 _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
 
Old October 16th, 2010, 10:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You also forgot to hook up the SelectedIndexChanged on the DDL.

This code:

Code:
 
<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True">
should be

Code:
<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ThemeList_SelectedIndexChanged">
Hope this helps,

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!
The Following User Says Thank You to Imar For This Useful Post:
ken evans (October 16th, 2010)
 
Old October 16th, 2010, 10:19 AM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Now it works!

Thanks Imar.
Ken





Similar Threads
Thread Thread Starter Forum Replies Last Post
Letting the user select a theme avi1 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 3rd, 2010 01:04 PM
Applying a user selected theme tariq BOOK: Beginning ASP.NET 4 : in C# and VB 4 July 4th, 2010 01:52 PM
Applying the User-Selected Theme Kerblamalot BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 January 29th, 2010 11:08 AM
Ch 6 - User-Selected theme not working jennyWren BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 16 December 21st, 2009 03:52 PM
Chapter 6 User-Selected Theme Question jabo2099 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 August 21st, 2008 03:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.