Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 25th, 2013, 04:50 PM
Banned
Points: 33, Level: 1
Points: 33, Level: 1 Points: 33, Level: 1 Points: 33, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Location: Atlanta, Georiga USA
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
  #2 (permalink)  
Old May 26th, 2013, 04:07 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
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!
Reply With Quote
  #3 (permalink)  
Old May 26th, 2013, 10:54 AM
Banned
Points: 33, Level: 1
Points: 33, Level: 1 Points: 33, Level: 1 Points: 33, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Location: Atlanta, Georiga USA
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #4 (permalink)  
Old May 26th, 2013, 11:29 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

>> 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
__________________
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!
Reply With Quote
  #5 (permalink)  
Old May 26th, 2013, 02:18 PM
Banned
Points: 33, Level: 1
Points: 33, Level: 1 Points: 33, Level: 1 Points: 33, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Location: Atlanta, Georiga USA
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Default 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>
Reply With Quote
  #6 (permalink)  
Old May 26th, 2013, 02:34 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
nudirection (May 26th, 2013)
  #7 (permalink)  
Old May 26th, 2013, 02:41 PM
Banned
Points: 33, Level: 1
Points: 33, Level: 1 Points: 33, Level: 1 Points: 33, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Location: Atlanta, Georiga USA
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Default

That was it!

Thanks much!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:27 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.