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

December 15th, 2009, 06:30 PM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ch 6 - User-Selected theme not working
Hi
I've read the posts here and tried all ways - but this swap just isn't working. Only one of the backgrounds is showing - ie the one whose name is included in the web.config page. The page is re-loading each time, but I'm only getting the one background.
I had to change the ID of the dropdown from "1stPreferredTheme" to "FirstPreferredTheme" as the program didn't seem to like it - ie line underneath in the VB page code.
I'm not getting an error.
Any ideas?
Thanks
|

December 16th, 2009, 05:20 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Did you write code in the Master Page and in the BasePage? And is your page inheriting that BasePage?
BTW: it's lst as in list, not 1st as in first. Check out the errata for that (and other) changes / errors / explanations in the book,
If that doesn't help, can you post the code for the relevant code files (master page, page and BasePage)?
Cheers,
Imar
|

December 16th, 2009, 04:47 PM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oops - of course btn, lst, txt (I forgot). The times new roman style of type in the book made it look like 1st - but, I should have known.
Anyway I seem to be going around in circles so here is the code -
BasePage.vb
Code:
Public Class BasePage
Inherits System.Web.UI.Page
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim preferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
If preferredTheme IsNot Nothing Then
Page.Theme = preferredTheme.Value
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.PreRender
If Me.Title = "" Then
Throw New Exception("Page title cannot be blank.")
End If
End Sub
End Class
Web_config
<pages theme ="Monochrome"> (amongst other default code)
MasterPage.master.vb
Code:
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub lstPreferredTheme_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstPreferredTheme.SelectedIndexChanged
Dim preferredTheme As HttpCookie = New HttpCookie("PreferredTheme")
preferredTheme.Expires = DateTime.Now.AddMonths(3)
preferredTheme.Value = lstPreferredTheme.SelectedValue
Response.Cookies.Add(preferredTheme)
Response.Redirect(Request.Url.ToString())
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.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 lstPreferredTheme.Items.FindByValue(selectedTheme) IsNot Nothing Then
lstPreferredTheme.Items.FindByValue(selectedTheme).Selected = True
End If
End If
End Sub
End Class
MasterPage.master
Code:
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" 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 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 a theme
<asp:DropDownList ID="lstPreferredTheme" runat="server" AutoPostBack="True" OnSelectedIndexChanged="lstPreferredTheme_SelectedIndexChanged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
</asp:DropDownList>
</div>
<div id="Footer">Footer Goes Here</div>
</div>
</form>
</body>
</html>
Thanks
|

December 16th, 2009, 06:41 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi jenny,
That all looks pretty much like it should except for one thing:
<asp:DropDownList ID="lstPreferredTheme" runat="server" AutoPostBack="True" OnSelectedIndexChanged="lstPreferredTheme_Selected IndexChanged">
The handler setup for OnSelectedIndexChanged is not needed (as the Code Behind already defines this using the Handles keyword) and would cause the code to trigger twice. However, I don't think that's causing the problem.
Can it be the Web Form you're trying this out on? Maybe that page does not inherit BasePage so PreInit which applies the theme is never called? Can you post the code for that page too?
Cheers,
Imar
|

December 17th, 2009, 08:17 AM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is default.aspx
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<h1>
Hi there visitor and welcome to Planet Wrox</h1>
<p class="Introduction">
We're glad you're paying a visit to <a href="http://www.PlanetWrox.com">
www.PlanetWrox.com</a> the coolest music community on the Internet.</p>
<p class="Introduction">
Feel free to have a <a href="Default.aspx">look around</a>, as there are lots of
interesting <b>reviews and concert pictures</b> to be found here.</p>
<p class="Introduction">
You can <a href="Login.aspx">log in</a> here.</p>
</asp:Content>
I have just noticed that I don't seem to have much on this page anymore. No reference to HTML etc.
Last edited by jennyWren; December 17th, 2009 at 08:19 AM..
|

December 17th, 2009, 08:26 AM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I remember now - I got rid of it when making the master page.
|

December 17th, 2009, 08:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
And can you post the Code Behind for this page?
Imar
|

December 17th, 2009, 08:55 AM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Finally works -
Within the Default.aspx. vb page I compared it to your example and found that the bit stating "Inherits BasePage" was assigned to
Code:
Partial Class _Default
Inherits System.Web.UI.Page
End Class
which was probably my default. I can't see it stated in the book.
Its a great example anyway. Theres always something small that causes problems - Thanks.
Last edited by jennyWren; December 17th, 2009 at 08:59 AM..
|

December 17th, 2009, 09:34 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yeah, that's what I though. Normally, this is how the inheritance change looks:
Code:
YourPage
System.Web.UI.Page
When you inherit BasePage, this changes to:
Code:
YourPage
BasePage
System.Web.UI.Page
That is, BasePage can do anyhting System.Web.UI.Page can. And YourPage can do anything BaseBase can.
If you leave out the BasePage, the behavior defined in that class never runs, and thus the theme is never applied.
Hope this clarifies things.
Cheers,
Imar
|

December 17th, 2009, 12:39 PM
|
Registered User
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello Imar,
I have the same problem as well (but in C#). Of coursed i followed every step in the book, read your comments above and also tryed to match every file in the Source folder to the Site folder (master, master.cs, default, default.cs, web.config, and BasePage). I also checked for Capital letters in the .cs files... but I have no idea what's wrong. can you please help me with that?
Thanks a lot!
|
|
 |