 |
| 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
|
|
|
|

April 3rd, 2013, 10:42 PM
|
|
Registered User
|
|
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

April 4th, 2013, 02:48 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

April 5th, 2013, 02:46 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

May 17th, 2013, 03:32 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
 |