 |
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
|
|
|
|

October 16th, 2010, 08:01 AM
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
|
|
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
|

October 16th, 2010, 09:13 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|

October 16th, 2010, 09:42 AM
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
|
|
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:
|
|

October 16th, 2010, 09:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|

October 16th, 2010, 10:04 AM
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
|
|
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)
{
}
}
|

October 16th, 2010, 10:13 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

October 16th, 2010, 10:19 AM
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 56
Thanks: 17
Thanked 2 Times in 2 Posts
|
|
Now it works!
Thanks Imar.
Ken
|
|
 |
|