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

March 11th, 2011, 12:44 PM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Can't make the example
Hi...
I'm learning ASP.Net, and there is example in the book (Beginning ASP.net 4 C# and VB ).
The example uses the Styles.css file in chapter 6 ... and the sidebar in the example must appear at the right side of the page, but it appears at the left side with me, and I don't know what's wrong!!  
please help me
The example is at page 200
and this is my code :
The master page :
Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" 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>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<link href="Styles/Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div id="PageWrapper">
<div id="Header" ><a id="A1" class="HeaderLink" href="~/" runat="server"></a> </div>
<div id="MenuWrapper">Menu Here</div>
<div id="MenuContent">
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="Sidebar">SideBar Here</div>
<div id="Footer">Footer Here</div>
</div>
</form>
</body>
</html>
The Styles.css :
Code:
#Header
{
background-color: #C0C0C0;
width: 844px;
height: 86px;
}
*
{
font-family: Arial;
}
h1
{
font-size: 20px;
}
.Introduction
{
font-style: italic;
color: #003399;
}
#PageWrapper
{
width: 844px;
}
#MenuWrapper
{
width: 844px;
}
#MainContent
{
width: 644px;
float: left;
}
#Sidebar
{
background-color: Gray;
width: 200px;
float: left;
}
#Footer
{
background-color: #C0C0C0;
width: 844px;
clear: both;
}
#MainContent a
{
color: #008000;
text-decoration: underline;
}
#MainContent a:visited
{
color: #FF0000;
text-decoration: underline;
}
#MainContent a:hover
{
color: #FFA500;
text-decoration: underline;
}
h1
{
padding: 0px;
margin: 0px 0px 10px 0px;
}
thanks alot 
|
|

March 11th, 2011, 06:49 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Your main content div is called MenuContent and not MainContent as it should.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

March 13th, 2011, 03:17 PM
|
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
Chapter 6 Themes: Dynamic Switching Failing
I'm a total newbie to ASP.NET (experienced in ColdFusion (9)) and enjoying the book, but suddenly I'm stuck in deep mud.
All my previous code examples are running well, but when I attempt to get Dynamically Switching Themes (pages 228-232) to run, they don't change when I select them with the drop down list.
Everything up to this point runs.
I can get the DarkGrey and Monochrome themes to change along with the associated images by changing the web.config code below to either Monochrome or DarkGrey:
<pages theme="Monochrome" styleSheetTheme="Monochrome"></pages>
but cannot dynamically change the themes in the browser with the drop down list. I thought this may be a cookie storage (security) issue with my browser, so I checked the code with another browser and feel I've eliminated local cookies storage as an issue.
I've also, carefully checked the code twice.
I'm wondering if there's a case issue with "PreferredTheme" in the code below in that this cookie is defined as PreferredTheme with an uppercase "P" where all other references in the code below have the "P" in lower case...
------------------code between------
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());
}
-------------------code between---------
Last edited by jpjamie; March 13th, 2011 at 03:40 PM..
|
|

March 13th, 2011, 03:41 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I'm wondering if there's a case issue with "PreferredTheme" in the code below in that this cookie is defined as PreferredTheme with an uppercase "P" where all other references in the code below have the "P" in lower case...
|
The two are, other than by name, not really related. One is a literal string referring to the cookies's name, and other is just a variable. You could rename the variable (preferredTheme) to anything you want and it wouldn't make a difference.
My guess is that the problem is elsewhere; either in the Master Page or in the BasePage. Can you post the markup of the Master Page and the code in BasePage.cs?
Imar
|
|

March 13th, 2011, 03:49 PM
|
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
MasterPage/Frontend.Master
Imar: Thanks for the FAST reply!
Here is the MasterPage/Frontend.Master code...
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Frontend.master.cs" Inherits="Site_MasterPages_Frontend" %>
<!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>The Front End Master</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div id="PageWrapper">
<div id="Header"><a id="A1" href="~/" runat="server"></a></div>
<div id="MenuWrapper">Menu Goes Here</div>
<div id="MainContent">
<asp:ContentPlaceHolder ID="cpMainContent" runat="server">
This is default text that shows up in content pages that don't
explicitly override it.
</asp:ContentPlaceHolder>
</div>
Select a Theme:<br />
<div id="Sidebar">
<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True"
onselectedindexchanged="ThemeList_SelectedIndexCha nged">
<asp:ListItem>Monochrome</asp:ListItem>
<asp:ListItem>DarkGrey</asp:ListItem>
</asp:DropDownList>
</div>
<div id="Footer">Footer Goes Here</div>
</div>
</form>
</body>
</html>
Here is the (codebehind) MasterPage/Frontend.Master/Frontend.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Site_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) &&
ThemeList.Items.FindByValue(selectedTheme) != null)
{
ThemeList.Items.FindByValue(selectedTheme).Selecte d = 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());
}
}
HERE is BasePage.cs
using System;
using System.Web;
public class BasePage : System.Web.UI.Page
{
private void Page_PreRender(object sender, EventArgs e)
{
if (this.Title == "Untitled Page" || string.IsNullOrEmpty(this.Title))
{
throw new Exception(
"Page title cannot be \"Untitled Page\" or an empty string.");
}
}
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
Page.Theme = preferredTheme.Value;
}
}
public BasePage()
{
this.PreRender += new EventHandler(Page_PreRender);
this.PreInit += new EventHandler(Page_PreInit);
}
}
Last edited by jpjamie; March 13th, 2011 at 03:59 PM..
Reason: Updating to add code.
|
|

March 13th, 2011, 03:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you post one more thing: the code behind of the page you're testing this on?
Imar
|
|

March 13th, 2011, 04:16 PM
|
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
The code behind
Thanks again!
I'm using Default.aspx and here's the code behind for this file which is named Default.aspx.cs...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
++++++++++++++++++++++++++
Just thought I'd provide you with my current web.config file...
<?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>
<compilation debug="false" targetFramework="4.0" />
<pages theme="Monochrome" styleSheetTheme="Monochrome"></pages>
</system.web>
</configuration>
Last edited by jpjamie; March 13th, 2011 at 04:24 PM..
Reason: Adding web.config
|
|

March 14th, 2011, 03:53 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Ah, there you have it:
Code:
public partial class _Default : System.Web.UI.Page
Your test page does not inherit BasePage. This means that PreInit is never fired and the theme is never set. Change System.Web.UI.Page to BasePage and it should work.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

March 14th, 2011, 06:33 PM
|
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
THANKS!
Quote:
Originally Posted by Imar
Ah, there you have it:
Code:
public partial class _Default : System.Web.UI.Page
Your test page does not inherit BasePage. This means that PreInit is never fired and the theme is never set. Change System.Web.UI.Page to BasePage and it should work.
Cheers,
Imar
|
WOW Thanks! That clears it up and I can move on!
|
|

March 14th, 2011, 07:03 PM
|
|
Authorized User
|
|
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
|
|
Monochrome Menu Question
Thanks again Imar for helping me run down the dropdown list issue with Basepage. The dropdown works now, however my menu at the top in monochrome extended to the right of the header about 50 pixels. I know this is minor, since the demonstration of dynamically changing themes is the whole point. I changed the code in monochrome.css so the header, footer etc. has a width of 890 which seems to fix the menu issue. Though the menu in monochrome isn't centered in the header, at least it's within the header now. Thanks for those comments in the monochrome.css as they help a lot!!
So will my width changes screw up anything coming up in the next chapters?
Last edited by jpjamie; March 14th, 2011 at 07:13 PM..
|
|
 |
|