Modifing a theme page at the Page_Init or Page_Load of my ma

Posted by Community Admin on 04-Aug-2018 16:56

Modifing a theme page at the Page_Init or Page_Load of my master page

All Replies

Posted by Community Admin on 29-Jul-2011 00:00

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>();
        
    


On the line "base.SetPageDirectives(handler, pageData);" there is the error : 
Erreur 10 'Telerik.Sitefinity.Web.PageRouteHandler' ne contient pas de définition pour 'SetPageDirectives'

In english : 'Telerik.Sitefinity.Web.PageRouteHandler' doesn't contain any definition for 'SetPageDirectives'

This is my first Sitefinity approach so sorry if the solution is easy.


Thank you in advance

Posted by Community Admin on 01-Aug-2011 00:00

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>();
        
    

In order to do this on PageInit you have to create a custom class which inherits from ASP.NET Page class then in the Init event you have to apply this logic:
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;

Since you are going to use a custom page type instead of having the handler variable you can set the theme to this. Once you are done with your custom page you can set Sitefinity pages to use it (set their code behind type, from their properties section).


All the best,
Radoslav Georgiev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 01-Aug-2011 00:00

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.

Posted by Community Admin on 01-Aug-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 01-Aug-2011 00:00

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.


Posted by Community Admin on 09-Aug-2011 00:00

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);

The following method is in my Template.Master.cs

protected override void OnLoad(EventArgs e)
base.OnLoad(e);
WebHelper.LoadTheme(this.Page);

However there is no sign of the themes css file appearing in the page.

Have I missed something here?

Cheers,
Steven.

Posted by Community Admin on 09-Aug-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed