p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 4th, 2009, 10:59 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cookie question

Hi,

In the code, you put the creation of cookie in the dropdownlistbox and it is working fine if the cookie is already present. I tried expiring the cookie, and re-running the app again, and so it crashes. The page_load event in the master page is getting the theme, and the Select Case is converting a null value in to lower case.

What will happen if the web app is run for the first time without the cookie stored in the user pc, nor any theme defined in web.config?

Thanks

Last edited by janilane : May 4th, 2009 at 11:13 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 5th, 2009, 04:53 AM
Imar's Avatar
Wrox Author
Points: 33,563, Level: 80
Points: 33,563, Level: 80 Points: 33,563, Level: 80 Points: 33,563, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
Default

Hi there,

I am not sure what the problem is. Take a look at the code in the Master Page:

Code:
HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
if (preferredTheme != null)
{
  selectedTheme = preferredTheme.Value;
}
This uses some defensive coding and only fires when the cookie is there and not null. Otherwise, preferredTheme keeps its default value of Page.Theme.

If that doesn't help, can you provide detailed steps on reproducing this problem?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old May 5th, 2009, 05:58 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

What I did:

1.In the code-behind of Master page SelectedIndexChanged, I set the cookie expiry value to -1 just to expire it. Ran the application, and select value from the list box. As expected it will throw an error but that's okay as I just want to expire the cookie;

preferredTheme.Expires = DateTime.Now.AddMonths(-1)

2.Set the cookie value again to 3 months (because I'm thinking of the scenario where this is the first time the application will run without any cookie in the machine) like this;

preferredTheme.Expires = DateTime.Now.AddMonths(3)

3. Removed any theme set in the web.config file, and ran the app. It threw an error. I guess because the app still depends on a DEFAULT theme even if the user has not selected any themes in the dropdown list when the app is run for the first time.

Nevermind this scenario, just experimenting with code...

Thanks again.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old May 6th, 2009, 04:35 PM
Imar's Avatar
Wrox Author
Points: 33,563, Level: 80
Points: 33,563, Level: 80 Points: 33,563, Level: 80 Points: 33,563, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
Default

Hi janilane,

I don't think it's related to the cookie but to this:
Quote:
Removed any theme set in the web.config file
The application assumes that a theme is configured in the web.config. This is always the default theme, which can be overridden by the user. Without a least a default theme, the site looks pretty ugly.

If you do want to proceed with this, you need to wrap the code that tries to find the item in the list and that sets the correct navigation items in another if statement:
Code:
if (selectedTheme != null)
{
  if (lstPreferredTheme.Items.FindByValue(selectedTheme) != null)
  {
    lstPreferredTheme.Items.FindByValue(selectedTheme).Selected = true;
  }
  switch (Page.Theme.ToLower())
  {
    case "darkgrey":
      Menu1.Visible = false;
      TreeView1.Visible = true;
      break;
    default:
      Menu1.Visible = true;
      TreeView1.Visible = false;
      break;
  }
}
Depending on what you want to do with the navigation in a "themeless page", you may need to change the logic about hiding or showing the Menu or the TreeView.

Hope this clarifies things a bit...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old November 2nd, 2009, 06:19 PM
Authorized User
Points: 208, Level: 4
Points: 208, Level: 4 Points: 208, Level: 4 Points: 208, Level: 4
Activity: 43%
Activity: 43% Activity: 43% Activity: 43%
 
Join Date: Jun 2009
Posts: 48
Thanks: 14
Thanked 1 Time in 1 Post
Default

FWIW, I had a similar problem to the original poster. I am building a site using structural concepts from this book - in my case allowing switching between several themes so that others can give opinions on the best one for a company site under development. I too could not figure why I had no default theme. For anybody searching this forum like me with the same problem, Imar's answer gives the clue. If you go to web.config, there is a default theme set

Code:
<pages theme="Monochrome">
Now I am sure this must have been covered in the book - but I had forgotten. Hope I am not stating the obvious - but I was floundering a bit on this one, so maybe it will help someone else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old November 3rd, 2009, 01:37 PM
Imar's Avatar
Wrox Author
Points: 33,563, Level: 80
Points: 33,563, Level: 80 Points: 33,563, Level: 80 Points: 33,563, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
Default

The book tells you to add and remove the theme at several different stages to see different behavior. I am not sure what the last action is: add or remove.

I am currently working on v4 of this book and about to start with the chapter on themes, so if anything comes up that clarifies this behavior, I'll post here with an update.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old November 4th, 2009, 07:52 AM
Authorized User
Points: 208, Level: 4
Points: 208, Level: 4 Points: 208, Level: 4 Points: 208, Level: 4
Activity: 43%
Activity: 43% Activity: 43% Activity: 43%
 
Join Date: Jun 2009
Posts: 48
Thanks: 14
Thanked 1 Time in 1 Post
Default

Another point on this one. When developing the site on your own computer, the cookie set is for localhost, and it looks for PreferredTheme. If you are working on any other site's development with a similar structure (BasePage with similar cookie request) make sure you clear that cookie first. Otherwise, for your new site, the browser will still be looking at localhost, will look for the cookie, look for the preferred theme you declared on the book's site, not find it, and throw an error.

A bit unusual maybe - but it flumoxed me for a few minutes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old November 5th, 2009, 04:18 AM
Imar's Avatar
Wrox Author
Points: 33,563, Level: 80
Points: 33,563, Level: 80 Points: 33,563, Level: 80 Points: 33,563, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,231
Thanks: 7
Thanked 203 Times in 201 Posts
Default

Yeah, good tip. On production servers this is not a problem as cookies are scoped to a domain. However, as you point out, multiple sites running on localhost will share cookies.

You could prefix your cookie keys with a unique name, like PW_ (for Planet Wrox). But then you need to remember to change that when you copy content from one site to another ;-)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cant find cookie.... lammy BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 February 21st, 2009 07:26 AM
Cookie prv299 ASP.NET 2.0 Professional 3 May 29th, 2008 12:26 PM
Can't delete cookie...! Snib Beginning PHP 9 July 15th, 2007 11:32 PM
Cookie and Session rinventive PHP How-To 0 December 6th, 2005 07:09 PM
Cookie Dictionary Question johne Classic ASP Basics 0 April 18th, 2005 06:57 PM



All times are GMT -4. The time now is 07:00 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc