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

Forums / Developing with Sitefinity / Change master page/ Set page template at runtme

Change master page/ Set page template at runtme

4 posts, 0 answered
  1. Panos Klaoudatos
    Panos Klaoudatos avatar
    8 posts
    Registered:
    14 Aug 2012
    19 Nov 2008
    Link to this post
    Hello,

    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)
            {
                base.OnPreInit(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
    Registered:
    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.

    Regards,
    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
    Registered:
    14 Sep 2008
    18 Dec 2008
    Link to this post
    Hi:

    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
    Registered:
    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:

    http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-bkaaec.aspx

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

    Gabe Sumner
    http://www.sitefinitywatch.com/

Register for webinar
4 posts, 0 answered