Wrox Programmer Forums
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion 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 April 3rd, 2013, 10:42 PM
Registered User
 
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 6 preselects correct item

First question,I'm confused on this method. I understand from reading the book that it is used when this is not a postBack and when user doesn't have a store cookie. However, I don't understand how the code is effecting the theme on the page. I don't see any code that use the value of selectedTheme to the website? See, in the Page_PreInit method it says Page.Theme=preferredTheme.value, which changes the theme of the page. But the page_load doesn't change any value outside the method that effect the web page. Or maybe I don't understand the code.

Second question, the public BasePage()
{
this.PreRender+= Page_PreRender;
This.preInit+=Page_PreInit;
}
So, basically, the asp server run the basepage constructor every time and then inside the constructor the methods are executed? But I thought constructors are used by other classes to change the values of the global variables of the class that contains the constructors?

Thanks for helping.

Last edited by hfl12; April 3rd, 2013 at 10:44 PM..
 
Old April 4th, 2013, 02:48 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's a bit unclear what code or method your first question refers to. However, the theme is not applied in the Master Page, but in the Base Page. The Master Page just serves as a means for the user to select a theme. The selection is then stored in a cookie which is then used in the Base Page.
Quote:
the asp server run the basepage constructor every time and then inside the constructor the methods are executed?
Not completely. When the constructor runs, that code identifies the methods that should be run on PreInit and PreRender. Then when the lifecycle of the page is executes, the methods associated with these events are executed. So, they are executed at PreInit and PreRender time respectively, not when the Page class is constructed.

Hope this helps. If not, please clarify your question a bit further.

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 April 5th, 2013, 12:56 PM
Registered User
 
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your respond. I wasn't sure if I could post the code.

if (!Page.IsPostBack)
{
string selectedTheme = Page.Theme;
HttpCookie preferredTheme = Request.Cookies.Get("preferredTheme");
if (preferredTheme != null)
{
selectedTheme = preferredTheme.Value;
}
if (!string.IsNullOrEmpty(selectedTheme))
{
ListItem item = ThemeList.Items.FindByValue(selectedTheme);
if (item != null)
{
item.Selected = true;
}
}
}

Can you tell me which part of this code store data for the master page?
 
Old April 5th, 2013, 02:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not sure what you mean with "store data for the master page". This code doesn't store any data; all it does is preselect the correct theme in the DropDownList based on the selection the user made earlier.

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 May 16th, 2013, 04:30 PM
Registered User
 
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry but when does this if(!page.isPostback), I posted above, run? is it right after the ThemeList_SelectedIndexChanged method or after the preInit ?Thanks
 
Old May 17th, 2013, 03:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It runs within Page_Load, which in turn runs in between PreInit and SelectedIndexChanged.

PreInit fires really early in the page life cycle. That's where you need to apply the theme based on the cookie's value (if any). Then Page_Load fires which tries to preselect the theme based on the cookie (if any) but only when the page is not posting back.

Then when you make a change in the drop down, PreInit fires, then Page_Load (but because of the !Page.IsPostback check no change is made to the drop down list and then finally SelectedIndexChanged fires which stores the cookies and performs a redirect.

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
Chapter 4 : Todo List Item, can't see text IAmCorbin BOOK: Professional Android 2 Application Development 1 June 29th, 2010 02:56 PM
Chapter 7 : Cannot update a cart item Qiaona Jin BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 January 3rd, 2009 05:23 AM
Chapter 5 GridView Selected Item stoverje BOOK: Professional ASP.NET 2.0 Design: CSS, Themes, and Master Pages ISBN: 978-0-470-12448-2 2 August 8th, 2008 03:11 PM
Chapter 15 - item.asp Joe Kerr BOOK: Beginning ASP 3.0 1 February 7th, 2006 06:42 PM





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