More in this section

Forums / General Discussions / Change template dynamically

Change template dynamically

3 posts, 0 answered
  1. Eric
    Eric avatar
    147 posts
    Registered:
    12 Nov 2007
    01 Sep 2010
    Link to this post
    I'd like to switch between two templates dynamically. For example, say I have two templates: Guest.Master and Authenticated.Master.

    Outside SF, I've set up a Page_PreInit via the Application_PreRequestHandlerExecute in the Global.asax that would switch the master page (like below). The templates would be known to (and hard-coded in) the code. The templates would also contain the same ContentPlaceHolders.

    protected void Application_PreRequestHandlerExecute(object src, EventArgs e) {
        Page p = this.Context.Handler as Page;
        if (p != null)
            p.PreInit += new EventHandler(Page_PreInit);
    }
     
    protected void Page_PreInit(object sender, EventArgs) {
        if (IsUserAuthenticated) {
            Page p = Context.Handler as Page;
            p.MasterPageFile = "~/MasterPages/Authenticated.master";
        }
    }

    There was a thread about switch the theme (http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/css-theme-selector.aspx). But I don't want to have to save/publish the page each time since this will be done on all page views and I'd hate to clutter up the page's revision history.

    Will this approach work with SF?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Sep 2010
    Link to this post
    Hi Eric,

    It would not be possible to switch any page parameters without saving changes made to objects that have been retrieved with a given manager and then committing the transaction. We are considering some options to make such changes during initialization, but in BETA it is not possible to change the theme/template without saving the changes.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Eric
    Eric avatar
    147 posts
    Registered:
    12 Nov 2007
    01 Sep 2010
    Link to this post
    I probably should have tried it out first before bothering anyone here, it works pretty smoothly.

    The Context.Handler is a Telerik.Sitefinity.Web.UI.SitefinityRadPage, which inherits from Telerik.Web.UI.RadAjaxPage, which inherits from System.Web.UI.Page, which has the MasterPageFile property which is settable in the PreInit event.

    protected void Application_PreRequestHandlerExecute(object src, EventArgs e) {
        Page p = Context.Handler as Page;
        if (p != null)
            p.PreInit += Page_PreInit;
    }
     
    private void Page_PreInit(object sender, EventArgs e) {
        Page p = Context.Handler as Page;
        if (p.MasterPageFile == "/Guest.Master" && DateTime.Now.Minute % 2 == 0)
            p.MasterPageFile = "/Auth.Master";
    }

    The DateTime.Now.Minute % 2 == 0 was just an easy way to test the switch.

    Both of my master pages have identical ContentPlaceHolders. Had it not worked, I could have written custom controls to handle the differences between authenticated users and guests, but this makes it simpler -- I think.

    I like Sitefinity.
3 posts, 0 answered