Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old April 8th, 2012, 04:32 AM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Fallowing your advice I'll give it a shot at it

Inside my Parent Master.page I have included this code -

Code:
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) && Themes.Items.FindByValue(selectedTheme) != null)
            {
                Themes.Items.FindByValue(selectedTheme).Selected = true;
            }

            if (Session["MyMasterPage"] != null)
            {
                ListItem li = Themes.Items.FindByText(Session["MyMasterPage"].ToString());
                if (li != null)
                {
                    li.Selected = true;
                }
            }
        }
    }
    protected void Themes_SelectedIndexChanged(object sender, EventArgs e)
    {
        HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
        preferredTheme.Expires = DateTime.Now.AddMonths(3);
        preferredTheme.Value = Themes.SelectedValue;
        Response.Cookies.Add(preferredTheme);
        Response.Redirect(Request.Url.ToString());

        Session["MyMasterPage"] = Themes.SelectedValue;
        Response.Redirect(Request.Url.ToString());
    }
}
and inside my BasePage.cs I have this -

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{

    private void Page_PreInit(object sender, EventArgs e)
    {
        HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
        if (preferredTheme != null)
        {
            Page.Theme = preferredTheme.Value;
        }

        /* ------------------------------------------------------ */

        this.MasterPageFile = GetMasterPageFileFromSession();
    }

    protected string GetMasterPageFileFromSession()
    {
        if (Session["MyMasterPage"] == null)
        {
            return "~/MasterPages/DarkViolet.master";
        }
        else
        {
            return Session["MyMasterPage"].ToString();
        }
    }

    /* ------------------------------------------------------ */

    private void Page_PreRender(object sender, EventArgs e)
    {
        if (this.Title == "Untitled Page" || string.IsNullOrEmpty(this.Title))
        {
            throw new Exception("Page title cannot be \"Untitled Page\" or an empty string.");
        }
    }

	public BasePage()
	{
        this.PreRender += new EventHandler(Page_PreRender);
        this.PreInit += new EventHandler(Page_PreInit);
	}
}

Now I know that the first part of the code is by fallowing your book tutoarial on how to switch between themes in Chapter 6.

The problem I'm facing is that whenever I run my website - nothing happens, no respons and no reload of the webpage att all.

Why is that????

Thanks in advance
Reply With Quote
  #12 (permalink)  
Old April 8th, 2012, 05:08 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,033
Thanks: 80
Thanked 1,582 Times in 1,559 Posts
Default

Hi there,

A few things I noticed / wondered....

1. Is Themes_SelectedIndexChanged being called? E.g. did you set AutoPostBack on the control to true? have you tried putting a breakpoint in the method and see if it gets hit?

2. Take a look at this:

Code:
HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
preferredTheme.Expires = DateTime.Now.AddMonths(3);
preferredTheme.Value = Themes.SelectedValue;
Response.Cookies.Add(preferredTheme);
Response.Redirect(Request.Url.ToString());

Session["MyMasterPage"] = Themes.SelectedValue;
Response.Redirect(Request.Url.ToString());
After you set the cookie, you are redirecting the user away from the page. This means that the code that sets the session is never called. Remove the first call to Redirect and the session value will be set.

3. Why are you using cookies and Session state at the same time? Both seem to do the same (store the selected theme) so I would choose one or the another but not both.

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!
Reply With Quote
  #13 (permalink)  
Old April 8th, 2012, 05:22 AM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I made sure that [ AutoPostback is set to "true" ] and I have deleted the first [ Response.Redirect(Request.Url.ToString()); ]

I belive now that the error lies with that [ Themes_SelectedIndexChanged] is never called for some reason, when I inserted a breakpoint it never registerd.
Reply With Quote
  #14 (permalink)  
Old April 8th, 2012, 05:41 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,033
Thanks: 80
Thanked 1,582 Times in 1,559 Posts
Default

Did you wire up the event in the markup? That is, does the DropDownList look like this:

<asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ThemeList_SelectedIndexCha nged">

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!
Reply With Quote
  #15 (permalink)  
Old April 8th, 2012, 05:49 AM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

This is my dropdownlist control

Code:
<asp:DropDownList ID="Themes" runat="server" name="Themes" AutoPostBack="True" 
        onselectedindexchanged="Themes_SelectedIndexChanged">
        <asp:ListItem>DarkViolet</asp:ListItem>
        <asp:ListItem>SereneLight</asp:ListItem>
      </asp:DropDownList>

thanks
Reply With Quote
  #16 (permalink)  
Old April 8th, 2012, 06:09 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,033
Thanks: 80
Thanked 1,582 Times in 1,559 Posts
Default

That looks alright to me, so the problem must be elsewhere. To avoid going back and forth a lot of times posting bits and pieces of relevant code, I created a small sample site and attached it to this post. It uses the Planet Wrox themes, two nested Master Pages and uses only cookies to switch both the theme and the master page (that is, I removed all Session related code).

Hope this example helps in figuring out why it doesn't work for you.

Cheers,

Imar
Attached Files
File Type: zip SwitchingThemesAndMasterPages.zip (79.6 KB, 2 views)
__________________
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!
Reply With Quote
  #17 (permalink)  
Old April 8th, 2012, 06:14 AM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks I'll check it out and let you know how it went

Thanks in advance
Reply With Quote
  #18 (permalink)  
Old April 8th, 2012, 07:16 AM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

If it's not to much trouble may I ask why I keep getting this error whenever I run your site example

Error-Messege:
http://imageshack.us/photo/my-images/534/errornny.jpg/
Reply With Quote
  #19 (permalink)  
Old April 8th, 2012, 07:52 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,033
Thanks: 80
Thanked 1,582 Times in 1,559 Posts
Default

Probably because the preferredTheme cookie doesn't have a value. Try moving it to inside the if block right above it....

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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Slay01 (April 8th, 2012)
  #20 (permalink)  
Old April 8th, 2012, 12:54 PM
Registered User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I want to thank you for all your help Mr. Imar

It's finally working now, I had to redo my entire site from scratch and now it's finally finally working.

Thanks again for all help.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
General question - Master Pages, Base pages, templates and OOP. flapjack BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 May 1st, 2010 09:33 AM
Master Pages micptpuser ASP.NET 2.0 Basics 6 January 8th, 2009 03:50 AM
Master Pages Jayahar ASP.NET 3.5 Basics 1 September 2nd, 2008 04:20 PM
VB: GridView & DetailsView in Master-Child Rich57 ASP.NET 2.0 Basics 1 May 29th, 2008 07:14 AM
Master Pages, Content Pages and CSS carliviris Visual Studio 2005 0 January 8th, 2008 04:56 PM



All times are GMT -4. The time now is 10:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.