Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
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 June 6th, 2012, 10:27 AM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default Chapter 6 - Letting the User Select a Theme

Hello,

First of all, I'd just like to say the book has been great help getting into ASP.NET, I've been hooked in the past few days to it and I couldn't ask for better, fantastic work.

Anyway, I've been having a few issues in this chapter, the book covers how to do this with a drop down list, but I wanted to try and do it with a couple of buttons, I have 2 buttons, one for each theme in my website, both the themes are working fine at page level, it's something I'm doing wrong with the code that isn't working.

Maybe you could give me some help with this? My two themes are "orangefresh" and "greenfresh", this is my master page 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 mpage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string selectedTheme = Page.Theme;
            HttpCookie webtheme = Request.Cookies.Get("webtheme");
            if (webtheme != null)
            {
                selectedTheme = webtheme.Value;
            }

        }
    }
    protected void orangefresh_Click(object sender, EventArgs e)
    {
        HttpCookie webtheme = new HttpCookie("webtheme");
        webtheme.Expires = DateTime.Now.AddMonths(3);
        webtheme.Value = "orangefresh";
        Response.Cookies.Add(webtheme);
        Response.Redirect(Request.Url.ToString());
    }

    protected void greenfresh_Click(object sender, EventArgs e)
    {
        HttpCookie webtheme = new HttpCookie("webtheme");
        webtheme.Expires = DateTime.Now.AddMonths(3);
        webtheme.Value = "greenfresh";
        Response.Cookies.Add(webtheme);
        Response.Redirect(Request.Url.ToString());
    }
}
Base Page:

Code:
using System;
using System.Web;
public class BasePage : System.Web.UI.Page
{
private void Page_PreInit(object sender, EventArgs e)
{
HttpCookie webtheme = Request.Cookies.Get("webtheme");
if (webtheme != null)
{
    Page.Theme = webtheme.Value;
}
}
public BasePage()
{
    this.PreInit += new EventHandler(Page_PreInit);
}
}
I followed the logic in the book and tried to change things around so it would fit the needs of buttons, but with no success.

Here is a pastebin of the master page markup:
http://pastebin.com/FCZ3ySZQ

I'm sorry if this is the wrong place to post in, I know it differs from the books content, I apologize if this isn't the right place for this.
 
Old June 6th, 2012, 11:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Don't worry about the place where you posted this. I think this is very on-topic.

That said, I tested your code and it works fine for me. Could it be that the page you're testing this on does not inherit BasePage, but the standard Page class instead? When that's the case, the theme is never applied.

Also, you could avoid some code duplication by changing this:

Code:
protected void orangefresh_Click(object sender, EventArgs e)
{
  HttpCookie webtheme = new HttpCookie("webtheme");
  webtheme.Expires = DateTime.Now.AddMonths(3);
  webtheme.Value = "orangefresh";
  Response.Cookies.Add(webtheme);
  Response.Redirect(Request.Url.ToString());
}
protected void greenfresh_Click(object sender, EventArgs e)
{
  HttpCookie webtheme = new HttpCookie("webtheme");
  webtheme.Expires = DateTime.Now.AddMonths(3);
  webtheme.Value = "greenfresh";
  Response.Cookies.Add(webtheme);
  Response.Redirect(Request.Url.ToString());
}
to this:
Code:
protected void orangefresh_Click(object sender, EventArgs e)
{
  SaveCookieAndRedirect("orangefresh");
}
protected void greenfresh_Click(object sender, EventArgs e)
{
  SaveCookieAndRedirect("greenfresh");
}
private void SaveCookieAndRedirect(string themeName)
{
  HttpCookie webtheme = new HttpCookie("webtheme");
  webtheme.Expires = DateTime.Now.AddMonths(3);
  webtheme.Value = themeName;
  Response.Cookies.Add(webtheme);
  Response.Redirect(Request.Url.ToString());
}
This way, you only need to maintain one method, and the click handlers simply call the SaveCookieAndRedirect method.

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!
 
Old June 6th, 2012, 11:44 AM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Imar! That was it! Thanks for your help, I was busting my head last night with my code and the whole inheritance thing went past my mind...

Again, many thanks.
 
Old June 10th, 2012, 06:40 AM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hey Imar,

I've been having a new complication with my page that I can't seem to understand, I'm now on the AJAX chapter and configured my contact form, similar to the one in the book.

Here is my ContactForm.ascx

Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="controls_ContactForm" %>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="style1" runat="server" id="FormTable">
    <tr>
        <td class="style2">
            Name</td>
        <td class="style4">
            <asp:TextBox ID="Name" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="Enter your name" ControlToValidate="Name" 
                CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            E-mail address</td>
        <td class="style4">
            <asp:TextBox ID="EmailAddress" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter an e‑mail address">*</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter a valid e‑mail address" 
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            E-mail address again</td>
        <td class="style4">
            <asp:TextBox ID="ConfirmEmailAddress" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ControlToValidate="ConfirmEmailAddress" CssClass="ErrorMessage" 
                Display="Dynamic" ErrorMessage="Confirm the e‑mail address">*</asp:RequiredFieldValidator>
            <asp:CompareValidator ID="CompareValidator1" runat="server" 
                ControlToCompare="EmailAddress" ControlToValidate="ConfirmEmailAddress" 
                CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Retype the e‑mail address">*</asp:CompareValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            Comments</td>
        <td class="style4">
            <asp:TextBox ID="Comments" CssClass="style3" TextMode="MultiLine" runat="server" Height="78px" 
                Width="291px"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                ControlToValidate="Comments" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter a comment">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            <asp:Button ID="Send" runat="server" Text="Send" onclick="Send_Click" />
            <br />
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                CssClass="ErrorMessage" />
        </td>
        <td>
            &nbsp;</td>
    </tr>
</table>
<asp:Label ID="Message" runat="server" CssClass="messagesent" Width="256" Height="286" Text="Message Sent" Visible="False" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="PleaseWait">
Please Wait...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
I called the form in my index.aspx file:

Code:
 <div id="popupContact">  
        <a id="popupContactClose">x</a>  
        <h1>Need to get in touch?</h1>  
        <p id="contactArea">  
            <uc1:ContactForm ID="ContactForm1" runat="server" />
        </p>  
    </div>
Those divs work as a jquery popup, so the form only appears after a click event as a sort of popup.

If I call this "ContactForm1" on my page, the theme selector somehow stops working, all of the code remained the same as in my last posts, themes start working again as soon as remove the form from my page... any ideas on why this is happening?

Thanks for your help.

EDIT: Here's what I also noticed, if I click one of the buttons to change my theme, the Form is somehow picking this up and displaying all of the VallidationSummary Errors...

Last edited by jdlferreira; June 10th, 2012 at 06:44 AM..
 
Old June 10th, 2012, 07:15 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I a not sure I understand the problem. Do you have a live example I can look at or a better problem description? Maybe there's some JavaScript issue somewhere that stops the postback from happening?

Quote:
if I click one of the buttons to change my theme, the Form is somehow picking this up and displaying all of the VallidationSummary Errors...
The buttons trigger the validation. Set CausesValidation to False on the two buttons to work around that.

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 June 10th, 2012, 07:18 AM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
The buttons trigger the validation. Set CausesValidation to False on the two buttons to work around that.
Yep! This was it, thanks again Imar, you're one cool dude.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6 pps 226-228 User Selected Theme Featheriver BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 20th, 2011 07:08 AM
weird behaviour - chapter 6 User-selected theme lornarmac BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 2nd, 2010 06:36 AM
Letting the user select a theme avi1 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 3rd, 2010 01:04 PM
(Chapter 6) applying user-selected theme in IE7 phoenixx ASP.NET 3.5 Basics 6 August 26th, 2008 09:49 AM
Chapter 6 User-Selected Theme Question jabo2099 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 August 21st, 2008 03:45 AM





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