 |
BOOK: Beginning ASP.NET 4.5 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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
|
|
|
|
|

May 25th, 2013, 04:50 PM
|
|
Banned
|
|
Join Date: May 2013
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Chapter 6 - Dynamically Switching Themes
Hi,
I've been doing OK up until this point; able to find my own mistakes. I am stuck on what went wrong here. The browser page cookie only remembers the drop down selection and does not change the theme. I've enabled the AutoPostBack option and compared the code to the book numerous times.
|
|

May 26th, 2013, 04:07 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
HI there,
Can you describe where you get stuck? The exercise is done in two parts, so maybe you simply haven't completed the part that assigns the theme in BasePage?
If you did write that code, can you then post the code for the master page (markup and code behind), the Basepage and the page you're testing this on? If you post code, can you please paste it in Notepad first to remove color coding and then use the forum editor's Code toolbar button to wrap your code in code tags. Otherwise, this forum messes up the code.
Cheers,
Imar
|
|

May 26th, 2013, 10:54 AM
|
|
Banned
|
|
Join Date: May 2013
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Dynamically Switching Theme code
That's entirely possible. I've been following your exercises in order though. Here's the code from the requested pages.
Frontend.master
Code:
<%@ Master Language="VB" CodeFile="Frontend.master.vb" Inherits="MasterPages_Frontend" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Planet Wrox</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="PageWrapper">
<header><a href="/"></a></header>
<nav>Menu Goes Here</nav>
<section id="MainContent">
<asp:ContentPlaceHolder ID="cpMainContent" runat="server">
</asp:ContentPlaceHolder>
</section>
<aside 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></aside>
<footer>Footer Goes Here</footer>
</div>
</form>
</body>
</html>
Frontend.master. vb
Code:
Partial Class MasterPages_Frontend
Inherits System.Web.UI.MasterPage
Protected Sub ThemeList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ThemeList.SelectedIndexChanged
Dim preferredTheme As HttpCookie = New HttpCookie("PreferredTheme")
preferredTheme.Expires = DateTime.Now.AddMonths(3)
preferredTheme.Value = ThemeList.SelectedValue
Response.Cookies.Add(preferredTheme)
Response.Redirect(Request.Url.ToString())
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim selectedTheme As String = Page.Theme
Dim preferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
If preferredTheme IsNot Nothing Then
selectedTheme = preferredTheme.Value
End If
If Not String.IsNullOrEmpty(selectedTheme) Then
Dim item As ListItem = ThemeList.Items.FindByValue(selectedTheme)
If item IsNot Nothing Then
item.Selected = True
End If
End If
End If
End Sub
End Class
BasePage. vb
Code:
Public Class BasePage
Inherits System.Web.UI.Page
Private Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
If String.IsNullOrEmpty(Me.Title) OrElse Me.Title.Equals("Untitled Page",
StringComparison.CurrentCultureIgnoreCase) Then
Throw New Exception(
"Page title cannot be ""Untitled Page"" or an empty string.")
End If
End Sub
End Class
Default.aspx
Code:
<%@ Page Title="Welcome to Planet Wrox" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>Hi there visitor and welcome to Planet Wrox</h1>
<p>We're glad you're paying a visit to <a href="http://www.PlanetWrox.com">www.PlanetWrox.com</a>, the coolest music community site on the Internet.</p>
<p class="Introduction">Feel free to have a <a href="Default.aspx">look around</a>, there are lots of interesting <strong>reviews and concert pictures</strong> to be found here.</p>
<p>You can <a href="Login.aspx">log in</a> here.</p>
</asp:Content>
err, I think I may see what you mean about the two-part exercise. I have not completed the 'Applying the User-Selected Theme' exercise yet. I'll continue with that and see what happens.
Thanks Imar.
Regards, Mark
|
|

May 26th, 2013, 11:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> I have not completed the 'Applying the User-Selected Theme' exercise yet
I was just going to refer to that ;-) Following that exercise should do the trick.
Cheers,
Imar
|
|

May 26th, 2013, 02:18 PM
|
|
Banned
|
|
Join Date: May 2013
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Applying the User-Selected Theme
Still getting the same result; defaults to Monochrome theme. Here's my base page file.
BasePage. vb
Code:
Public Class BasePage
Inherits System.Web.UI.Page
Private Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
If String.IsNullOrEmpty(Me.Title) OrElse Me.Title.Equals("Untitled Page",
StringComparison.CurrentCultureIgnoreCase) Then
Throw New Exception(
"Page title cannot be ""Untitled Page"" or an empty string.")
End If
End Sub
Private Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim preferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
If preferredTheme IsNot Nothing Then
Dim folder As String = Server.MapPath("-/App_Themes/" & preferredTheme.Value)
If System.IO.Directory.Exists(folder) Then
Page.Theme = preferredTheme.Value
End If
End If
End Sub
End Class
and 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>
<pages theme="Monochrome"/>
<compilation debug="false" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
|
|

May 26th, 2013, 02:34 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Take a look at this:
Quote:
|
Dim folder As String = Server.MapPath("-/App_Themes/" & preferredTheme.Value)
|
You're using a dash (-) instead of a tilde (~) in front of the App_Themes folder....
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

May 26th, 2013, 02:41 PM
|
|
Banned
|
|
Join Date: May 2013
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
That was it!
Thanks much!
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Dynamically Switching Themes |
robbiehaas |
BOOK: Beginning ASP.NET 4 : in C# and VB |
5 |
January 30th, 2014 05:52 PM |
| Dynamically switching themes |
KingJames978 |
BOOK: Beginning ASP.NET 4 : in C# and VB |
15 |
April 22nd, 2012 11:16 AM |
| Switching Themes |
allken |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
9 |
September 18th, 2011 10:03 AM |
| ChP 6 - Themes not switching |
Cooler |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
5 |
February 17th, 2011 03:26 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 |
|
 |
|