More in this section
Forums / Developing with Sitefinity / Change master page/ Set page template at runtme

Change master page/ Set page template at runtme

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
4 posts, 0 answered
  1. Panos Klaoudatos
    Panos Klaoudatos avatar
    8 posts
    14 Aug 2012
    19 Nov 2008
    Link to this post

    I need to change the master page (set page template in SiteFinity terms) of a custom page at runtime, based on a case scenario.

    To be more specific, I have a created a page base class MyPage that inherits from InternalPage.

    Now, let's say we have two templates in Sitefinity, default and someOtherMaster.
    By design, pageA.aspx has the default template applied to it.

    Depending on a logical test, I need to change the master page of pageA.aspx at runtime, but I don't want to save the chnage in the database.

    What I did, is override the PreInit function of MyPage like this:

    protected override void OnPreInit(EventArgs e)

                if (LogiclTestIsTrue)
                    Telerik.Cms.CmsManager manager = new Telerik.Cms.CmsManager();
                    IPageTemplate template = manager.GetTemplate("someOtherMaster");
                    this.CmsPage.Staged.SetTemplate(template.ID, template.Theme);

    That doesn't work as SetTemplate has to be used when editing/saving the page.

    Is this case scenario possible?
    If so, how do I overcome this problem?

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    12 Sep 2017
    19 Nov 2008
    Link to this post
    Hi Panos Klaoudatos,

    Take a look at the following forum thread set the theme and masterpage during the OnPreInit method. The last posts reveal how this could be achieved.

    I hope this helps.

    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Allen
    Allen avatar
    1 posts
    14 Sep 2008
    18 Dec 2008
    Link to this post

    The link that was provided doesn't appear to link to a thread regarding this issue, but instead opens up a list of forums.  I am hopeful that this thread will help me too.  I'm currently trying to figure out how to programmatically / dynamically change the Master Page of a CMS managed page at runtime based on a query string.  With no physical page to put code behind, I am unable to access the PreInit event within the master page.

    Would you provide an updated link?

  4. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    09 Sep 2007
    18 Dec 2008
    Link to this post
    Hi Allen,

    I too am having trouble with that link.  However, I found the following pages while searching old forum posts:

    This post mentions a MasterPageFile property.  I didn't test it, but this property perhaps could be used to switch the template.

    Gabe Sumner

Register for webinar
4 posts, 0 answered