Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 15th, 2011, 08:33 AM
Registered User
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default some question about try it out on page 227

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class 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());
}
}

1. if (!Page.IsPostBack) pagepostback returns true if these is a current postback going on right? so if true this dont get executed. the not operator always confuses me for some reason.
2. string selectedTheme = Page.Theme; where does the page object come from

3.HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme"); "PreferredTheme" I assume this iscome from the selected index part where it assigns a name variable to the object preferred theme. Right
4. the rest of the code i am confused also. Will post mny other questions once i understand this first part.
Reply With Quote
  #2 (permalink)  
Old August 15th, 2011, 08:48 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

1) Yes. It fires when the page loads the first time, which means there's no postback. This code is functionally equivalent to:

if (Page.IsPostBack == false)

2) It's a property on the Page class itself (the one in which this code executes): http://msdn.microsoft.com/en-us/libr...trol.page.aspx

3.
>> I assume this iscome from the selected index part where it assigns a name variable to the object preferred theme

Not sure I understand this question. The cookies collection is an indexed collection where you can retrieve cookies by their name. PreferredTheme in this case is the name of the cookie that the code looks for. If there's a cookie by that name it's returned, otherwise the Get method returns null.

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!
Reply With Quote
  #3 (permalink)  
Old August 15th, 2011, 09:34 AM
Registered User
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
request.cookies.get is a static method. IS this right? ANd it is returning what?
i am assuming it is returning the object made from the selected index function. which is stored on your hard drive in the interenet cookie section right. AND then it is putting the name "preferredtheme" into the httpcookie object preferred theme.

so page is a static class. or is it some sort of asp.net thing. i guess i will read mroe about this later.
Reply With Quote
  #4 (permalink)  
Old August 15th, 2011, 10:01 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
request.cookies.get is a static method. IS this right? ANd it is returning what?
No, it's not. It's a collection property (not a method) on an *instance* of the Page class. If it were static, all users in your system would share the same cookies ;-) The type of the collection is HttpCookieCollection. You can read more about cookies in ASP.NET here:

http://msdn.microsoft.com/en-us/libr...t.cookies.aspx
http://msdn.microsoft.com/en-us/library/ms178194.aspx

Quote:
i am assuming it is returning the object made from the selected index function. which is stored on your hard drive in the interenet cookie section right. AND then it is putting the name "preferredtheme" into the httpcookie object preferred theme.
Yeah, sort of. It's a collection filled by ASP.NET for each request based on the cookies that the browser submits. Each item in the collection is an HttpCookie that contains data you as a developer have assigned to it (as well as other cookies that may have been set for the domain). In the SelectedIndexChanged method of the Master Page you created an HttpCookie, added it the *Response.Cookies* collection using PreferredTheme as the key which in turn caused the cookie to be sent to the client. Then on each new request that cookie is sent back to the server where it's available in the Request.Cookies collection.

Quote:
so page is a static class
No, it's not. or all users would be sharing the same page. Each request for an ASPX page results in a new instance of the Page class being created. The code you write in the code behind then works against that instance.

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
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
master page question barakros BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 January 30th, 2010 12:31 PM
Dumb Question for Page 8 litlmike BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 6 October 26th, 2009 10:21 PM
Registration Page Question Don Bricker BOOK: Beginning ASP.NET 2.0 and Databases 0 April 6th, 2009 02:34 PM
Chapt 9, Pg 227. Error? ericfields6483 BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 October 2nd, 2005 12:46 PM
redirect page question Edward King Javascript 1 March 16th, 2004 11:28 AM



All times are GMT -4. The time now is 05:40 AM.


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