Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
Password Reminder
Register
| FAQ | Members List | 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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
Old August 3rd, 2009, 01:22 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
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.
Reply With Quote
  #4 (permalink)  
Old August 5th, 2009, 08:59 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



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


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