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

September 17th, 2010, 08:07 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok this is the code of the basePage:
Code:
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
this.PreInit += new EventHandler(BasePage_PreInit);
this.PreRender += new EventHandler(BasePage_PreRender);
}
void BasePage_PreInit(object sender, EventArgs e)
{
HttpCookie PreferedTheme = Request.Cookies.Get("PreferedTheme");
if (PreferedTheme != null) {
Page.Theme = PreferedTheme.Value;
}
}
void BasePage_PreRender(object sender, EventArgs e)
{
if (this.Title == "Untitled Page" || string.IsNullOrEmpty(this.Title))
{
throw new Exception("Enter title for this page");
}
}
}
and this is the code of the master page:
Code:
public partial class MasterPages_Frontend : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
ThemeList.Items.FindByValue(Page.Theme).Selected=true;
}
protected void ThemeList_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie PreferedTheme = new HttpCookie("PreferedTheme");
PreferedTheme.Expires = DateTime.Now.AddMonths(3);
PreferedTheme.Value = ThemeList.SelectedValue;
Response.Cookies.Add(PreferedTheme);
Response.Redirect(Request.Url.ToString());
}
}
|
|

September 17th, 2010, 08:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I think I see what's going on. Take a look at this:
Code:
protected void Page_Load(object sender, EventArgs e)
{
ThemeList.Items.FindByValue(Page.Theme).Selected=true;
}
This code executes every time the page loads. It means it also runs when you select a new item, and *before* the selected value is stored in a cookie. This effectively resets the selected value back to the current theme, and thus nothing changes. Take a look at page 229, step 5 for the correct code example that checks for !Page.IsPostBack.
Does this help?
Cheers,
Imar
|
|

September 17th, 2010, 08:14 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes but when i change the drop down list it creates a cookie right? then it re-request the page, so the pre init sets the page theme, but my theme isn't changed this is the first problem , the second problem the drop down list should read the new value, but the page theme is not taking the new drop down list value.
|
|

September 17th, 2010, 08:16 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Take another look at my previous reply. You reset the drop down's value *before* the cookie is created. So, the current theme is reselected in the drop down, and then the cookie created with the current theme, not the one the user selected.
Imar
|
|

September 17th, 2010, 08:26 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What Event Happens before the other? PageLoad or Preinit?????
|
|

September 17th, 2010, 08:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
PreInit. But PreInit still works with the old cookie, as the new one isn't set until SelectedIndexChanged of the DDL...
|
|

September 17th, 2010, 08:31 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
so the theme should change on the second time i fire the drop down list event or third time, it's not changed at anytime, that is what i mean....
|
|

September 17th, 2010, 08:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I know what you mean, but you don't get what I mean. Here's what happens:
- Let's say the current theme is Monochrome.
- You pick DarkGrey from the drop-down list.
- The Page posts back. PreInit fires and sets the theme; still to Monochrome because the cookie hasn't changed yet
- Page_Load in the Master Page fires. Because you didn't add the check for Page.IsPostBack, the selected value in the DLL is reset from DarkGrey to Monochrome again, effectively clearing the user's selection
- SelectedIndexChanged fires. It stores Monochrome in the cookie (as that is now the value of the DDL) and redirects. You really want it to store DarkGrey here, but it has been reset in the previous step
- PreInit fires and assigns the theme. Since the cookie was set to Monochrome in the previous step, that theme is now applied again.
Does this make any sense? Why don't you try it out and see if it works?
Imar
|
|

September 17th, 2010, 08:46 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OUUUUUUCH .... CRYSTAL :)  
|
|

September 17th, 2010, 09:23 AM
|
|
Authorized User
|
|
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have tried it, and it worked fine :)
but when i change the theme to Dark Grey using this method, looks like the page's design is ruined and both themes are getting into each other, but when getting back to monochrom it works fine, what is that ? thank you very much Imar
|
|
 |
|