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

April 30th, 2011, 11:21 AM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chap 6 user selected theme
Hi there,
I'm working in C#. My select theme page seems to be storing and retrieving the cookie as the dropdown displays my last selected value, however the theme being displayed doesn't change.
Thanks for any help.
BASE PAGE CODE
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 an empty string.");
}
}
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
Page.Theme = preferredTheme.Value;
}
}
public BasePage()
{
this.PreRender += new EventHandler(Page_PreRender);
this.PreInit += new EventHandler(Page_PreInit);
}
}
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>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="PageWrapper">
<div id="Header">Header goes here</div>
<div id="MenuWrapper">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
ShowStartingNode="False" />
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"
Orientation="Horizontal" StaticEnableDefaultPopOutImage="False">
<StaticMenuItemStyle ItemSpacing="10px" />
</asp:Menu>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
ShowExpandCollapse="False">
</asp:TreeView>
</div>
<div id="MainContent">
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="Sidebar">Select a theme:
<asp:DropDownList ID="themeChoice" runat="server" AutoPostBack="True"
onselectedindexchanged="themeChoice_SelectedIndexChanged">
<asp:ListItem>Dark Gray</asp:ListItem>
<asp:ListItem>Monochrome</asp:ListItem>
</asp:DropDownList>
</div>
<div id="Footer">footer</div>
</div>
</div>
</form>
</body>
</html>
MASTER PAGE CODE BEHIND
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 (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (themeChoice.Items.FindByValue(selectedTheme) != null)
{
themeChoice.Items.FindByValue(selectedTheme).Selec ted = true;
}
}
}
protected void themeChoice_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = themeChoice.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
}
TESTING ON DEFAULT.aspx (HERE IS CODE BEHIND)
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)
{
}
}
Thanks for the remarkably speedy response!
Last edited by granola911; April 30th, 2011 at 11:49 AM..
|
|

April 30th, 2011, 11:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You pasted the code for the BasePage twice. Can you post the missing code and markup for the Master Page?
Also, can you post the code behind of the page you're testing this on?
Imar
|
|

April 30th, 2011, 12:22 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I've edited my original post rather then create a new entry. Thanks for any help 
|
|

April 30th, 2011, 12:40 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Ah, there we have it:
Code:
public partial class _Default : System.Web.UI.Page
You page is not inheriting BasePage, so the Theme is never applied. Once you change it to:
Code:
public partial class _Default : BasePage
it should work.
Cheers,
Imar
|
|

May 1st, 2011, 06:34 AM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Again, Imar.
I changed my default page to inherit the base page and still nothing. I am really stumped on this one.
Thanks for your help :)
|
|

May 1st, 2011, 09:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
That's strange. I just copied your code into my project and it worked after I made a small change. I changed
Code:
<asp:ListItem>Dark Gray</asp:ListItem>
to
Code:
<asp:ListItem>DarkGrey</asp:ListItem>
The Grey versus Gray spelling might not be the problem (as long as you named the theme accordingly), but the space between Dark and grey might be the issues.
If that doesn't make a difference, try clearing all your cookies and then make sure your theme is set up correctly. You can try that out by commenting out the code in the BasePage that sets the theme and then manually set the them to DarkGrey / DarkGray in web.config.
Does it work then? And are you sure the page you're testing this on inherits BasePage? Each page can have its own inherits option, so make sure you're inheriting the BasePage on all relevant pages as explained in the book.
Cheers,
Imar
|
|

May 1st, 2011, 10:15 AM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
my theme is named Dark Gray and my dropdown code is:
<asp:DropDownListID="themeChoice"runat="server"AutoPostBack="True"onselectedindexchanged="themeChoice_SelectedIndexChanged">
<asp:ListItem>Dark Gray</asp:ListItem>
<asp:ListItem>Monochrome</asp:ListItem>
</asp:DropDownList>
Which looks ok to. I tried clearing the cookies too, but no change.
Thanks again :)
|
|

May 1st, 2011, 10:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, I know, but the space might be causing the issue.
Try debugging the application (see Chapter 17) and see what value your cookie contains and whether it's null or not. Also, check out your anti-virus software and see if it's blocking your cookies.
Otherwise, can you zip up a copy of your site and post it on-line somewhere?
Cheers,
Imar
|
|
 |