Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 March 7th, 2009, 01:14 AM
Authorized User
 
Join Date: Mar 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
Question ThemeSelector with ImageControl

I have changed the ThemeSelector control from chapter 2 to change when a ImageControl is clicked. I have the code working; however, I have to click the image twice, or click the image and navigate to another page to get the theme to change.

Does anyone know why I do not get the theme change on the first click?

--- Ryan

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using RC.RPC_Group;
namespace RC.RPC_Group.UI.Controls
{
    public partial class ThemeSelector : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void blueTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            string themeName = "blueMidnight";
            Globals.ThemesSelectorID = themeName;
        }
        protected void redTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            string themeName = "maroonBlood";
            Globals.ThemesSelectorID = themeName;
        }
        protected void goldTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            string themeName = "goldLabel";
            Globals.ThemesSelectorID = themeName;
        }
}
}
 
Old March 7th, 2009, 01:47 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

This is because the theme gets selected on the server side, but is not applied until the next page is served.

If you want the theme to change immediately, you need to include functionality in the OnPreInit event handler of each page that checks if the control that caused the last postback is the ThemeSelector. You can do this by examining the Forms collection of the request. If it is, then you need to set the Theme of the page to the new theme that was selected.

The best way to include this functionality is to override the OnPreInit event in a base page that all site pages would inherit.

If you look at the BasePage.cs file in TheBeerHouse, it shows clearly how to do this.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 7th, 2009, 01:52 AM
Authorized User
 
Join Date: Mar 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
Default

I did that I thougt. Below is my code for BasePage.cs

Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace RC.RPC_Group.UI
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            string themeName = Globals.ThemesSelectorID;
            if (themeName.Length > 0)
            {
                    this.Theme = themeName;
            }
            base.OnPreInit(e);
        }
    }
}
 
Old March 7th, 2009, 02:02 AM
Authorized User
 
Join Date: Mar 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
Default

If I write the code like this, the this.theme is NULL?

Code:
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            string themeName = Globals.ThemesSelectorID;
            if (themeName.Length > 0)
            {
                this.Theme = this.Request.Form[themeName];
            }
            base.OnPreInit(e);
        }
    }
 
Old March 7th, 2009, 02:19 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Your original logic is flawed.

What you are doing in your ImageButton event handlers is setting Globals.ThemeSelectorID to the name of the current theme.

What you WANT to do is to set Globals.ThemeSelectorID to the ID of a control that will be available in the Request.Form collection. Then, you can get that control from Request.Form.

Since you are NOT setting Globals.ThemeSelectorIDto the ID of a control, then of course Request.Form[Globals.ThemeSelectorID] will always be a null.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 7th, 2009, 03:29 AM
Authorized User
 
Join Date: Mar 2009
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Ok, I changed my BasePage and ThemeSelector. ThemeSelector now sets Globals.ThemeSelectorID to the UniqueID of the ImageButton. However, this.Theme = this.Request.Form[id]; and this.Session and ["CurrentTheme"] = this.Theme; are still Null.

Do I need to change thos code since I am now using a ImageButton, not a drop down list?
if (this.Request.Form["__EVENTTARGET"] == id &&
!string.IsNullOrEmpty(this.Request.Form[id]))


Code:
namespace RC.RPC_Group.UI
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            string id = Globals.ThemesSelectorID;
            if (id.Length > 0)
            {
                // if this is a postback caused by the theme selectors dropdownlist,
                // retrieve the selected theme and unse it for the current page request
                if (this.Request.Form["__EVENTTARGET"] == id &&
                    !string.IsNullOrEmpty(this.Request.Form[id]))
                {
                    this.Theme = this.Request.Form[id];
                    this.Session["CurrentTheme"] = this.Theme;
                }
                else
                {
                    if (this.Session["CurrentTheme"] != null)
                        this.Theme = this.Session["CurrentTheme"].ToString();
                }
            }
            base.OnPreInit(e);
        }
    }
}
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using RC.RPC_Group;
namespace RC.RPC_Group.UI.Controls
{
    public partial class ThemeSelector : System.Web.UI.UserControl
    {
        string themeName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Globals.ThemesSelectorID.Length == 0)
                Globals.ThemesSelectorID = blueMidnight.UniqueID;
        }
        protected void blueTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            themeName = "blueMidnight";
            Globals.ThemesSelectorID = blueMidnight.UniqueID;
        }
        protected void redTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            themeName = "maroonBlood";
            Globals.ThemesSelectorID = maroonBlood.UniqueID;
        }
        protected void goldTheme_Box_Click(object sender, ImageClickEventArgs e)
        {
            themeName = "goldLabel";
            Globals.ThemesSelectorID = goldLabel.UniqueID;
        }
}
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Customize ThemeSelector rpcasey001 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 March 7th, 2009 01:08 AM
themeselector doesn't show on PlainYellow Rachel BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 January 7th, 2009 11:05 PM
ThemeSelector class error xrica32 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 August 14th, 2007 08:20 PM
Chapter 2 ThemeSelector - Puzzled Porjung BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 June 11th, 2007 07:43 AM





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