1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Modifing a theme page at the Page_Init or Page_Load of my master page

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

7 posts, 1 answered
  1. Pierre-Jean
    Pierre-Jean avatar
    8 posts
    Registered:
    29 Jul 2011
    29 Jul 2011
    Link to this post
    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
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    01 Aug 2011
    Link to this post
    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
  3. Pierre-Jean
    Pierre-Jean avatar
    8 posts
    Registered:
    29 Jul 2011
    01 Aug 2011
    Link to this post
    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.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Aug 2011
    Link to this post
    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
    Answered
  5. Pierre-Jean
    Pierre-Jean avatar
    8 posts
    Registered:
    29 Jul 2011
    01 Aug 2011
    Link to this post
    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.


  6. Steven Harrap
    Steven Harrap avatar
    13 posts
    Registered:
    26 Sep 2012
    09 Aug 2011
    Link to this post
    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.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Aug 2011
    Link to this post
    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
7 posts, 1 answered