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

Forums / Developing with Sitefinity / Issue changing template on the fly

Issue changing template on the fly

2 posts, 1 answered
  1. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    26 Aug 2009
    Link to this post
    Hi all, I am attempting to change the template of my pages on the fly. I used information in these two posts to create this logic:

    1 public class CustomPage : Telerik.Cms.Web.InternalPage 
    2
    3     protected override void OnPreInit(EventArgs e) 
    4     { 
    5         base.OnPreInit(e); 
    6         var gsAuth = new GSAuthentication(); 
    7         var cookie = gsAuth.GetCookie(); 
    8  
    9         var messaging = new GSMessaging(); 
    10  
    11  
    12         if (cookie != null
    13         { 
    14             var dataLayer = new Auth(); 
    15             var auth = dataLayer.GetAuthEntry(new Guid(cookie.Value)); 
    16  
    17             if (auth != null
    18             { 
    19                 CmsSiteMapNode currentNode = (CmsSiteMapNode)SiteMap.CurrentNode; 
    20                 CmsManager manager = new CmsManager(); 
    21  
    22                 if (currentNode != null
    23                 { 
    24                     IPage thisPage = manager.GetPage(currentNode.PageID); 
    25                     IPageTemplate theTemplate = manager.GetTemplate(auth.PracType == "035" ? "gsow-content-chiro" : "gsow-content-medical"); 
    26  
    27                     thisPage.Staged.SetTemplate(theTemplate.ID, "gsow"); 
    28                     manager.SavePage(thisPage); 
    29                 } 
    30                 else 
    31                 { 
    32                     messaging.MsgBox("Current Node is null"); 
    33                 } 
    34             } 
    35             else 
    36             { 
    37                 messaging.MsgBox("Auth OBJ is null"); 
    38             } 
    39         } 
    40     } 
    41

    This is the error I am getting, can anyone provide some assistance?

    Server Error in '/sf_gsow' Application.

    You cannot change a value of an object that is not in transaction.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: Nolics.ORMapper.Base.NolnetException: You cannot change a value of an object that is not in transaction.

    Source Error: 

    Line 43:      IPageTemplate  theTemplate = manager.GetTemplate(auth.PracType == "035" ? "gsow-content-chiro" : "gsow-content-medical");
    Line 44:
    Line 45:      thisPage.Staged.SetTemplate(theTemplate.ID,"gsow");
    Line 46:      manager.SavePage(thisPage);
    Line 47:  }

    Source File: c:\Program Files\telerik\Sitefinity3.6 Community\WebSites\sf_gsow\App_Code\CustomPage.cs    Line: 45 

    Stack Trace: 

    [NolnetException: You cannot change a value of an object that is not in transaction.]
    Nolics.ORMapper.Base.OdbClassHelper.CheckAssign(IOdbView clView, String propertyName, Object oldValue, Object newValue) +170
    Telerik.Cms.Data.StagedPage.set_TemplateID(Guid value) +121
    Telerik.Cms.Data.StagedPage.SetTemplate(Guid templateId, String theme) +310
    CustomPage.OnPreInit(EventArgs e) in c:\Program Files\telerik\Sitefinity3.6 Community\WebSites\sf_gsow\App_Code\CustomPage.cs:45
    System.Web.UI.Page.PerformPreInit() +31
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +282
     
    Version Information: Microsoft .NET Framework Version:2.0.50727.4016; ASP.NET Version:2.0.50727.4016
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    26 Aug 2009
    Link to this post
    Hello andersleet,

    First you should call public IPage GetPage(Guid id, bool forEdit); You could also try getting the page again before setting the template.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
2 posts, 1 answered