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 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 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 September 7th, 2010, 06:45 AM
Registered User
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically Switching Themes

After completing the exercises on pages 227 & 231, I run into an issue with what appears to be a conflict with the CSS code for the DarkGrey & Monochrome themes. If I have Monochrome set in the web.config file ( <pages theme="Monochrome" styleSheetTheme="Monochrome"></pages>), the DarkGrey theme on the dropdown list appears incorrect when selected. If I have DarkGrey set in the web.config file, the Monochrome them on the dropdown list appears incorrect when selected.

The CSS is fine code is correct, as the themes appear fine when selected in web.config file but not when dynamically switching themes. How do I correct this?

Thanks!
Reply With Quote
  #2 (permalink)  
Old September 7th, 2010, 07:11 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look at page 231, step 4 and the paragraph that follows. It explains the reason and a fix for this problem.

Cheers,

Imar
---------------------------------------------------------------------------------------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter.

Author of Beginning ASP.NET 4 : in C# and VB, Beginning ASP.NET 3.5 : in C# and VB, and ASP.NET 2.0 Instant Results.
While typing this post, I was listening to: Girl I Love You by Massive Attack (From the album: Heligoland) What's This?
Reply With Quote
  #3 (permalink)  
Old January 30th, 2014, 04:15 PM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2014
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi!
I have the exact same issue, however, I have deleted the css-include-line.

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" styleSheetTheme="Monochrome"></pages>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>
frontend.master
Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Frontend.master.cs" Inherits="MasterPages_Frontend" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <script src="/Scripts/modernizr-2.7.1.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" OnSelectedIndexChanged="ThemeList_SelectedIndexChanged">
                    <asp:ListItem>Monochrome</asp:ListItem>
                    <asp:ListItem>DarkGrey</asp:ListItem>
                </asp:DropDownList></aside>
            <footer>Footer Goes Here</footer>
        </div>
    </form>
</body>
</html>
Frontend.master.cs
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))
            {
                ListItem item = ThemeList.Items.FindByValue(selectedTheme);
                if (item != null)
                {
                    item.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());
    }
}
BasePage.cs
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)
        {
            string folder = Server.MapPath("~/App_Themes/" + preferredTheme.Value);
            if (System.IO.Directory.Exists(folder))
            {
                Page.Theme = preferredTheme.Value;
            }
        }
    }

    private void Page_PreRender(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.Title) || this.Title.Equals("Untitled Page", StringComparison.CurrentCultureIgnoreCase))
        {
            throw new Exception(
            "Page title cannot be \"Untitled Page\" or an empty string.");
        }
    }
    public BasePage()
    {
        this.PreRender += Page_PreRender;
        this.PreInit += Page_PreInit;
    }
}
I can't see whats wrong?
Reply With Quote
  #4 (permalink)  
Old January 30th, 2014, 04:45 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look at this:

Code:
<pages theme="Monochrome" styleSheetTheme="Monochrome"></pages>
You also have the styleSheetTheme set. So when you dynamically set the DarkGrey theme, both themes are applied.

Just remove styleSheetTheme from web.config and it should work.

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
The Following User Says Thank You to Imar For This Useful Post:
pydis (January 30th, 2014)
  #5 (permalink)  
Old January 30th, 2014, 05:31 PM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2014
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ohh...

So, to get VS to display the layout correctly in designmode, I need the styleSheetTheme="", but I can't use it in production?

Seems a bit odd, but hey :)

Anyway, thanks!
Reply With Quote
  #6 (permalink)  
Old January 30th, 2014, 05:52 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You can use it in production if you wanted to. Take another look at the intro section on themes where the differences between the two are explained. For the Planet Wrox site, you can't mix the two, because only the Theme (and not the StyleSheetTheme) is overridden dynamically at runtime.

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
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
Switching To entity framework luckystar BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 July 31st, 2009 09:52 PM
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
Switching list content dynamically ... asearle XSLT 1 September 24th, 2006 11:41 AM
Switching TableAdapters striker9 ASP.NET 2.0 Basics 3 March 29th, 2006 11:11 AM



All times are GMT -4. The time now is 12:22 AM.


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