Wrox Programmer Forums
|
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
 
Old October 3rd, 2010, 03:10 AM
Registered User
 
Join Date: Oct 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Letting the user select a theme

hi,

I was going through the book and cant seem to get the programmatic change of themes to work Pages 222-228 of chapter 6.

Below is the code from my MasterPage.Master file. On stepping through the code I found that the if (preferredTheme != null) and the if (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null) are always null. So it never enters these conditions. The selected theme is being stored in the Cookie(i checked the cookie value in FireFox), but it seems that it cannot be read back. Nothing happens when I select a different theme from the dropdown list. the page just postsback but the theme remains unchanged and is the same as the one specified in the web.config.

Please let me know if any further details are needed.

<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

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 (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null)
{
lstPreferredTheme.Items.FindByValue(selectedTheme) .Selected = true;
}
}
}

protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie preferredTheme = new HttpCookie("Preferred Theme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = lstPreferredTheme.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<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 class="HeaderLink" href="-/" runat="server">Header goes here</a></div>
<div id = "MenuWrapper">Menu goes here</div>
<div id = "MainContent">
<asp:ContentPlaceHolder ID ="cpMainContent" runat="server"></asp:ContentPlaceHolder>
</div>
<div id="Sidebar">Select your theme<asp:DropDownList ID="lstPreferredTheme"
runat="server" AutoPostBack="True"
onselectedindexchanged="lstPreferredTheme_Selected IndexChanged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
<asp:ListItem>ForMe</asp:ListItem>
</asp:DropDownList>
</div>
<div id= "Footer">Footer goes here</div>
</div>
</form>
</body>
</html>
 
Old October 3rd, 2010, 06:05 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 there,

Take a look at this:

Code:
 
Page_Load
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
Code:
 
SelectedIndexChanged
HttpCookie preferredTheme = new HttpCookie("Preferred Theme");
There's a space between Preferred and Theme in the code that sets the cookie. You thus set a cookie that's different from the one you're expecting when you try to read it again in Page_Load.

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!
 
Old October 3rd, 2010, 01:04 PM
Registered User
 
Join Date: Oct 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Imar! Removing the space solved the issue.





Similar Threads
Thread Thread Starter Forum Replies Last Post
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
VB.net 2005 User theme datagrid alex1985 Visual Studio 2005 7 September 11th, 2007 01:34 PM





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