Wrox Programmer Forums
|
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 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 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.
 
Old August 15th, 2011, 08:48 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,

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!
 
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.
 
Old August 15th, 2011, 10:01 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!





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





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