Hi,

I have several themes that can be applied to pages on a website based on a users preferences. The problem is that the only way I know how to apply a theme to a website is to set the Page.Theme object in the page's PreInit event. This requires that code be placed on each and every page's PreInit event.

Is there any way to apply a theme globally (as in to all pages in a website) in just one location, such as a master page or the global.asax?

yes you can do it via web.config file as follows :

<configuration>
<system.web>
<pages theme="yourThemeName" />
</system.web>
</configuration>

The reason that solution would not work for me is that theme must be set based on user preferences stored in some sort of datasource, right now an XML file, and thus would have to be set programmatically (rather than through the web.config file).

The solution I found does the trick. It's code to be placed in the global.asax file, which is exactly where i wanted it to be:

void Application_PreRequestHandlerExecute(object sender, EventArgs e)

    {

        Page page = Context.Handler as Page;
        String theme = "MyTheme";
        if (page != null)

        {

            page.Theme = theme;

        }

    }

where the theme variable is set to the value retrieved from the XML file

The reason that solution would not work for me is that theme must be set based on user preferences stored in some sort of datasource, right now an XML file, and thus would have to be set programmatically (rather than through the web.config file).

The solution I found does the trick. It's code to be placed in the global.asax file, which is exactly where i wanted it to be:

void Application_PreRequestHandlerExecute(object sender, EventArgs e)

    {

        Page page = Context.Handler as Page;
        String theme = "MyTheme";
        if (page != null)

        {

            page.Theme = theme;

        }

    }

where the theme variable is set to the value retrieved from the XML file

Hay good it works!
But is it posiible to use the same code in Application_Start() event so that it could be called only once at start?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.