Modifing a theme page at the Page_Init or Page_Load of my master page
Hello,
I would like to know if it is possible to modify the theme of a page at runtime but for every page that is loading.
For instance, i would like to use an url parameter to apply a theme on a page.
I think this should be done into the Page_init or the Page_Load of my master page, but I really don't know how to do it.
I was taking a look at the example shown in the documentation, but i don't think it feets my needs and I've got a problem with this example. I've got a compiling error in the following code :
using
Telerik.Sitefinity.Web;
using
System.Web.UI;
using
Telerik.Sitefinity.Pages.Model;
using
Telerik.Sitefinity.Web.UI;
using
Telerik.Sitefinity.Abstractions;
using
Telerik.Microsoft.Practices.Unity;
namespace
SitefinityWebApp.DomainControl
public
class
CustomRouteHandler : PageRouteHandler
protected
override
void
SetPageDirectives(Page handler, IPageData pageData)
base
.SetPageDirectives(handler, pageData);
var themeName =
"MDC"
;
ThemeController.SetPageTheme(themeName, handler);
public
static
void
RegisterType()
ObjectFactory.Container.RegisterType<PageRouteHandler, CustomRouteHandler>();
Hello Pierre-Jean,
The approach to do this is from a route handler, or the Init event in the page life cycle. There have been some changes in the Route handlers, and we are in the process of updating our documentation. Here is a version of the route handler that will work:
using
System.Web;
using
System.Web.UI;
using
Telerik.Microsoft.Practices.Unity;
using
Telerik.Sitefinity.Abstractions;
using
Telerik.Sitefinity.Web;
using
Telerik.Sitefinity.Web.UI;
using
Telerik.Sitefinity.Configuration;
using
Telerik.Sitefinity.Web.Configuration;
namespace
SitefinityWebApp
public
class
CustomRouteHandler: PageRouteHandler
protected
override
IHttpHandler BuildHttpHandler(System.Web.Routing.RequestContext requestContext)
if
(!ThemeController.IsBackendPage())
var handler = (Page)
base
.BuildHttpHandler(requestContext);
var theme =
"Metro"
;
var frontendThemes = Config.Get<AppearanceConfig>().FrontendThemes;
ThemeElement themeElement;
frontendThemes.TryGetValue(theme,
out
themeElement);
if
(themeElement.Path.StartsWith(
"~/App_Themes/"
))
handler.Theme = theme;
handler.Items[ThemeController.ThemeKey] = theme;
else
handler.Items[ThemeController.ThemeKey] = theme;
return
handler;
else
return
base
.BuildHttpHandler(requestContext);
public
static
void
RegisterType()
ObjectFactory.Container.RegisterType<PageRouteHandler, CustomRouteHandler>();
var frontendThemes = Config.Get<AppearanceConfig>().FrontendThemes;
ThemeElement themeElement;
frontendThemes.TryGetValue(theme,
out
themeElement);
if
(themeElement.Path.StartsWith(
"~/App_Themes/"
))
handler.Theme = theme;
handler.Items[ThemeController.ThemeKey] = theme;
else
handler.Items[ThemeController.ThemeKey] = theme;
Thank you very much for this answer.
Just a last little question : though this theme should be applied to all the page in my site, and all the pages will use my master page as well,
isn't it possible to put that code into the Page_Init of the Master Page ?
Apparently, the master page doens't implement the Theme property.
I'm not very experienced in Asp.Net yet, and I don't know if it is possible to retrieve a Page object into the Page_Init of the Master Page ?
Thank you again.
Hello Pierre-Jean,
We use ASP.NET routing and this is why you need to use route handler. You can use ThemeController.SetPageTheme in a user or custom control, or in another place where you have access to the Page object which should allow you to set the theme as well.
protected override void OnLoad(EventArgs e)
base.OnLoad(e);
ThemeController.SetPageTheme("MyTheme", this.Page);
All the best,
Ivan Dimitrov
the Telerik team
Ok, thank you again for this really fast answer.
I will take a look at this and try to improve my knowledge in asp.net to make a better use of all of this stuff.
Hi Ivan
I'm also trying to set the theme at runtime. I have the following public static method that will (eventually) choose the theme based on the location of the page in the site tree.
public
static
void
LoadTheme(Page page)
ThemeController.SetPageTheme(
"Services"
, page);
protected
override
void
OnLoad(EventArgs e)
base
.OnLoad(e);
WebHelper.LoadTheme(
this
.Page);
Hello Steven,
There should not be a problem with the css. Can you observe the request to the css files with fire bug and see the response? You can also try to add a query string parameter theme=my_theme_name_here and see if the theme will apply in this case correctly.
Kind regards,
Ivan Dimitrov
the Telerik team