Change template dynamically

Posted by Community Admin on 03-Aug-2018 16:51

Change template dynamically

All Replies

Posted by Community Admin on 01-Sep-2010 00:00

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 (www.sitefinity.com/.../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?

Posted by Community Admin on 01-Sep-2010 00:00

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

Posted by Community Admin on 01-Sep-2010 00:00

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.

This thread is closed