Wrox Programmer Forums
|
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
 
Old May 4th, 2009, 09:59 PM
Authorized User
 
Join Date: Apr 2009
Posts: 19
Thanks: 3
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 10:13 PM..
 
Old May 5th, 2009, 03:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
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!
 
Old May 5th, 2009, 04:58 PM
Authorized User
 
Join Date: Apr 2009
Posts: 19
Thanks: 3
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.

 
Old May 6th, 2009, 03:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
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!
 
Old November 2nd, 2009, 06:19 PM
Authorized User
 
Join Date: Jun 2009
Posts: 70
Thanks: 18
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.
 
Old November 3rd, 2009, 01:37 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
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!
 
Old November 4th, 2009, 07:52 AM
Authorized User
 
Join Date: Jun 2009
Posts: 70
Thanks: 18
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.
 
Old November 5th, 2009, 04:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
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!





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 11:26 AM
Can't delete cookie...! Snib Beginning PHP 9 July 15th, 2007 10: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 05:57 PM





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