tags:

views:

242

answers:

2

I put a drop down box on my master page with hardocoded theme values, and called it lstThemeChooser.

I want to be able to set the page theme using it. I read that I should put this in each of my pages:

protected void Page_PreInit(object sender, EventArgs e)
{
    Page.Theme = Request["lstThemeChooser"];
}

However the Request is null, so no theme is set.

The drop down box is set to autopostback=True.

Any ideas what I am doing wrong, or if this is somehow completely impossible?

(asp.net)

+1  A: 

You cannot do that in your Masterpage. You have to do that in all of your Pages. I would suggest sub classing the Page Object:

namespace MyNamespace.Mycontrols 
{
    public class Page : System.Web.UI.Page
    {
        public Page()
        {
            this.PreInit += new EventHandler(Page_PreInit);
        }

        void Page_PreInit(object sender, EventArgs e)
        {
            // Apply Theme
            this.Theme = Request["lstThemeChooser"];
        }
    }
}

EDIT: Using that class

public partial class MyPage: MyNamespace.Mycontrols.Page
{
    ...
}
Arthur
I want the dropdown box to appear in the header, at the very top right of my pages though, and the header is controlled by the master page. I'm afraid I don't quite understand your answer, could you explain what you mean about sub classing the page object? Sorry..
SLC
In your Masterpage you can control the controls logic. BUT: Only on each Page you are able to change the Theme. This is because only the Page, not the Masterpage, has the events PreInit and InitializeCulture. Subclassing means, that you create such a Class I provided as an example. Then, on each Page you have to change the baseclass. I edited my answer.
Arthur
I see, thank you very much. The only odd thing is, before I had the control in my master page but the page_preinit event in my page that uses the master page, but the Request was simply returning null... Is there something I am missing?
SLC
Yes - there may be an issue. If your list is in a naming container, then the ID is not "lstThemeChooser" but maybe something like this: "ctrl0. lstThemeChooser". Set a Breakpoint and inspect the Request Object.
Arthur
Thanks, you're fantastic!
SLC
I give up, I can't find what the request is called, even after breakpointing and examining the Request object :(
SLC
A: 

See this thread.

This might help you.

Manish