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 September 17th, 2010, 08:07 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


}
 
Old September 17th, 2010, 08:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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 September 17th, 2010, 08:14 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old September 17th, 2010, 08:16 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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 September 17th, 2010, 08:26 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What Event Happens before the other? PageLoad or Preinit?????
 
Old September 17th, 2010, 08:29 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

PreInit. But PreInit still works with the old cookie, as the new one isn't set until SelectedIndexChanged of the DDL...
__________________
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 September 17th, 2010, 08:31 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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....
 
Old September 17th, 2010, 08:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
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 September 17th, 2010, 08:46 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OUUUUUUCH .... CRYSTAL :)
 
Old September 17th, 2010, 09:23 AM
Authorized User
 
Join Date: Sep 2010
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Theme won't change in new content page lustigon BOOK: Beginning ASP.NET 4 : in C# and VB 1 June 21st, 2010 10:03 PM
Page Theme not sticking... gymwalker ASP.NET 3.5 Basics 4 November 14th, 2008 04:17 PM
Setting Theme in Master Page rodmcleay General .NET 12 April 9th, 2008 05:24 AM
Theme Selector wont pick up other theme Tawanda BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 May 4th, 2007 08:44 AM
page theme zhugeliang ASP.NET 2.0 Basics 1 January 9th, 2007 02:40 AM





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