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 August 3rd, 2009, 12:54 AM
Authorized User
 
Join Date: Jul 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 7 page261 as it relate to Chapter6 page223

Hello Imar,

I'm a little confused between Server.Transfer and Response.Redirect. I tried to use Server.Transfer instead of Response.Redirect on page 223 just to see what would happen, and it's not what I expected, and I can't come up with an explanation. For example I select "DarkGrey" and I see "DarkGrey in the variable "preferredTheme" and it actually does add the cookie with a value of "DarkGrey" (as I expected).

Code:
protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
    {
           HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
            preferredTheme.Expires = DateTime.Now.AddMonths(3);
      preferredTheme.Value = lstPreferredTheme.SelectedValue;
      Response.Cookies.Add(preferredTheme);
            //Response.Redirect(Request.Url.ToString());
      Server.Transfer(Request.Url.PathAndQuery);
    }

When I get to the code below and after the cookie request, the preferredTheme is still "Monochrome" (Something I did not expect). If I close the website and reopen it it shows "DarkGrey"


Code:
private void Page_PreInit(object sender, EventArgs e)
  {
    HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
    
    if (preferredTheme != null)
    {
      // Test to see if theme is still available (maybe it's been discontinued)
      string rootPath = Server.MapPath("~/App_Themes");
      var theme = System.IO.Directory.GetDirectories(rootPath, preferredTheme.Value);
      if (theme.Length > 0)
      {
        Page.Theme = preferredTheme.Value;
      }
    }
  }
Can you help me understand what is going on?

Thank you.
 
Old August 3rd, 2009, 01:22 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 there,

Server.Transfer takes place at the server, so the Request collection with your new cookie is not updated. Here's what the code / page does:

1. Handle DDL change
2. Add cookie to Response.Cookies
3. Transfer to new page
4. This new page sets the theme based on Request.Cookies which still contains the old value
5. The browser receives the cookie in Response.Cokkies and submits it with the next request in Request.Cookies.

To see what I mean set a breakpoint in the Transfer line and add the following to your Watch window:
Response.Cookies["PreferredTheme"].Value
Request.Cookies["PreferredTheme"].Value

You'll see the two cookie collections contain different values.

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!
 
Old August 4th, 2009, 01:27 PM
Authorized User
 
Join Date: Jul 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,

I finally get it.
 
Old August 5th, 2009, 08:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. Glad I could help...


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
chapter6.mysql mikezile BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 8 December 21st, 2007 07:34 AM
Chapter6 delete.php ghitar1 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 September 2nd, 2006 12:21 PM
How to relate tables in ASP .NET busybee BOOK: ASP.NET Website Programming Problem-Design-Solution 1 April 2nd, 2006 04:36 PM
How to Relate Tables in ASP .NET busybee ASP.NET 1.0 and 1.1 Basics 4 March 29th, 2006 01:25 AM
How to relate a double indexed table? izz SQL Server 2000 1 August 18th, 2003 06:21 AM





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