Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : 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 February 10th, 2015, 06:32 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2014
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 6 - localhost Cookies Issue

I've been working through the book, and I've hit a snag in the Try It Out on page 226 "Letting the User Select a Theme".

My cookie doesn't seem to be created. I can load the page, and select the theme from the drop down, which causes the refresh. But if I re-open the page from another tab, or close the browser and re-open it, it defaults back to Monochrome.

I've tried in both Chrome and IE, on both my home computer, and work computer. I don't see anything in my cookie settings that would cause it to be blocked.

I've done some searching on the issue, and I've seen some posts that suggest that cookies will not work if you are loading a page from http://localhost, which suggest using 127.0.0.1 instead, or setting up something in the hosts file, but 127.0.0.1 won't load the page for me (comes back with a "Bad Request - Invalid Host Name").

I also can't seem to change the the site URL in Visual Studio, possibly because I'm using Visual Studio Express. If I could do that, I might be able to get 127.0.0.1 working, which might let the cookies work. As it is, I'm not sure how to progress from here.

Any suggestions?
 
Old February 11th, 2015, 06:44 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can you post the code for your page and code behind? Also, if you already added code to apply the theme, can you post that too?

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 February 11th, 2015, 12:45 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2014
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Frontend.master
Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Frontend.master.cs" Inherits="MasterPages_Frontend" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
    <script src="/Scripts/modernizr-2.7.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
      <div id="PageWrapper">
        <header><a href="/"></a></header>
        <nav>Menu Goes Here</nav>
        <section id="MainContent">
          <asp:ContentPlaceHolder ID="cpMainContent" runat="server">
          </asp:ContentPlaceHolder>
        </section>
        <aside id="Sidebar">
          Select a Theme <br />
          <asp:DropDownList ID="ThemeList" runat="server" AutoPostBack="True">
            <asp:ListItem>Monochrome</asp:ListItem>
            <asp:ListItem>DarkGrey</asp:ListItem>
          </asp:DropDownList>
        </aside>
        <footer>Footer Goes Here</footer>
      </div>
    </form>
</body>
</html>
Frontend.master.cs
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))
        {
          ListItem item = ThemeList.Items.FindByValue(selectedTheme);
          if (item != null)
          {
            item.Selected = true;
          }
        }
      }
    }
    protected void ThemeList_SelectedIndexChanged(object sender, EventArgs e)
    {
      HttpCookie preferredTheme = new HttpCookie("PreferredTheme");
      preferredTheme.Expires = DateTime.Now.AddMonths(3);
      preferredTheme.Value = ThemeList.SelectedValue;
      Response.Cookies.Add(preferredTheme);
      Response.Redirect(Request.Url.ToString());
    }
}
I haven't added the code to apply the theme yet.
 
Old February 14th, 2015, 03:41 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Your drop down is missing the following code:

OnSelectedIndexChanged="ThemeList_SelectedIndexCha nged"

That way, the code to set the cookie is never set.

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!
The Following User Says Thank You to Imar For This Useful Post:
elbkhm (February 17th, 2015)
 
Old February 17th, 2015, 12:33 PM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2014
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Awesome, that was the issue. :)

Thank you very much for your help Imar!




Similar Threads
Thread Thread Starter Forum Replies Last Post
Classic ASP issue not able to run it on localhost sudeep.nigam Classic ASP Basics 0 September 3rd, 2013 03:21 AM
Chapter 5 on localhost M3rcuryKT BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 1 February 23rd, 2011 09:40 PM
Chapter 19 and localhost ClarkNaylor BOOK: Beginning ASP.NET 4 : in C# and VB 0 July 17th, 2010 02:19 PM
sudden localhost port issue ceric67 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 March 5th, 2009 05:48 PM
ASP.NET Duplicate Cookies Issue jtoth Classic ASP Components 3 March 23rd, 2005 12:28 AM





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