More in this section

Forums / Developing with Sitefinity / Pages API - Create Page with UserControls

Pages API - Create Page with UserControls

12 posts, 0 answered
  1. Dave Ranck
    Dave Ranck avatar
    14 posts
    Registered:
    30 Oct 2003
    12 Oct 2009
    Link to this post
    I am using the Pages API to create pages to deliver Generic content mixed with UserControls with an ID property that will retieve data based on that ID. The UserControl is not editable when cmsmode=edit, however all of the UserControls display "Index was outside the bounds of the array" when in edit mode. The page works as expected in the live site or preview mode, but it appears that these UserControls are not "Loaded" in edit mode. I'm not sure what the issue is, but is there a way to load the UserControl as if the page was created manually with a custom UserControl from the Tool Box?

    Using the following to create the page:
                    // create a new page with the   
                    // Name of the page and ID of the root page  
                    ICmsPage newPage = manager.CreatePage(pageName.Replace(" ""-"), page.ID);  
                    newPage.Title = pageName;  
                    newPage.Description = pageName;  
     
                    // Create a staged page from the new page  
                    IStagedPage staged = newPage.Staged.CheckOut();  
     
                    // set the template  
                    IPageTemplate template = manager.GetTemplate("LocationDetails");  
     
                    // set the theme  
                    staged.SetTemplate(template.ID, "21CDefault");  
     
                    // Add the Location Image UserControl  
                    newPage = (ICmsPage)manager.GetPage(newPage.ID, true);  
                    LocationPhotoBase imageControl = (LocationPhotoBase)LoadControl("UserControls/Location_Detail/LocationImage.ascx");  
                    imageControl.PageId = newPage.ID;  
                    imageControl.LocationId = locationId;  
                    newPage.Staged.AddControl("ContentPlacePhoto", imageControl);  
                    manager.SavePage(newPage);  
     
                    // Add the Location Services UserControl  
                    newPage = (ICmsPage)manager.GetPage(newPage.ID, true);  
                    LocationServicesBase servicesControl = (LocationServicesBase)LoadControl("UserControls/Location_Detail/LocationServices.ascx");  
                    servicesControl.PageId = newPage.ID;  
                    servicesControl.LocationId = locationId;  
                    newPage.Staged.AddControl("ContentPlaceServices", servicesControl);  
                    manager.SavePage(newPage);  
     
                    // Add the left content (generic_content)  
                    newPage = (ICmsPage)manager.GetPage(newPage.ID, true);  
                    GenericContent gcLeft = new GenericContent();  
                    gcLeft.Content = "";  
                    newPage.Staged.AddControl("ContentPlaceLeftContent", gcLeft);  
                    manager.SavePage(newPage);  
     
                    // add the right content (generic_content)  
                    newPage = (ICmsPage)manager.GetPage(newPage.ID, true);  
                    GenericContent gcRight = new GenericContent();  
                    gcRight.Content = "";  
                    newPage.Staged.AddControl("ContentPlaceRightContent", gcRight);  
                    manager.SavePage(newPage);  
     
                    // get ref to the new page  
                    // call CheckIn and publish the page  
                    newPage = (ICmsPage)manager.GetPage(newPage.ID, true);  
                    newPage.Staged.CheckIn();  
                    newPage.Publish();  
                    manager.SavePage(newPage); 
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Oct 2009
    Link to this post
    Hello Mike Brady,

    As I see from your code you are working with Page ID values, but in edit mode the Page ID is null and this is the reason why the controls are not created only in edit mode. For instance if you use some other controls as RadRotator or RadScheduler you will experience the same problem further more you will get an error message that we overcome on Render method where we shows a user friendly message - "The control cannot be previewed in Design mode"

    All the best,
    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.
  3. Dave Ranck
    Dave Ranck avatar
    14 posts
    Registered:
    30 Oct 2003
    12 Oct 2009
    Link to this post
    How would I set a "Friendly message" in this case? I don't need to display the usercontrol content in edit mode, but a friendly message informing the user that the items are not editable would be nice.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Oct 2009
    Link to this post
    Hi Mike Brady,

    Below is a sample code that illustrates how to create a friendly messeage

    protected override void InitializeControls(Control controlContainer)
          {
              if (Page == null || DesignMode)
              {
                  Label fMessage = new Label();
                  fMessage .Text = "This control cannot be previewed in Design mode";
                  //  HERE HIDE OR CONTROL - FOR INSTANCE - RADROTATOR
                  this.RadDotatorControl.Visible = false;
                  this.Controls.Add(fMessage);
              }
              else
              {
               // WE ARE NOT IN DESIGN MODE AND WE SHOULD RENDER AND BIND THE CONTROL
              }
          }


    Kind 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.
  5. Dave Ranck
    Dave Ranck avatar
    14 posts
    Registered:
    30 Oct 2003
    13 Oct 2009
    Link to this post
    Perhaps i'm missing where I would implement this, obviously not in the control, the master page, overriding a different event? Somewhere else?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    13 Oct 2009
    Link to this post
    Hi Mike Brady,

    In user controls you can override render and add your custom message as shown below:

    protected override void Render(HtmlTextWriter writer)
    {
        if (Page == null || DesignMode)
        {
            writer.Write("This control cannot be previewed in Design mode");
        }
    }

    I hope that this helps.

    Greetings,
    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.
  7. Dave Ranck
    Dave Ranck avatar
    14 posts
    Registered:
    30 Oct 2003
    13 Oct 2009
    Link to this post
    Thanks for sticking with me Ivan,
    The problem is that the user control is not being loaded in edit mode, therefore the render override will not run (in edit mode). Only the error, "Index was outside the bounds of the array" is displayed in the place of each user control. Generic Content controls are displayed correctly.
    What is the difference between using the designer to drag a custom user control from the toolbox and using the API to place the controls in the template containers?

    Thanks
  8. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    15 Oct 2009
    Link to this post
    Hello Mike,

    I have sent you an example control, which creates pages that can be edited just fine in Edit Page mode. Please check the support thread which you have opened. 

    Kind regards,
    Georgi
    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.
  9. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    09 Dec 2009
    Link to this post
    I'm having the exact same problem.  Any chance you can post the solution in the forum?  It doesn't make sense to me that the control renders correctly in the Preview and published mode, but fails in the edit mode.  Especially considering that in my case, the controls really are just reading properties and setting them as properties on child controls.
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Dec 2009
    Link to this post
    Hello mimholt,

    Actually it make sense, because in Edit mode the page is null. Sitefinity's pages does not exists physically and when you are working in Edit mode and you have a controls that expects a page an error is thrown.  So you can replace the error message with a user friendly message overriding Render and checking the QueryString

    if(this.Page.ClientQueryString == "cmspagemode=edit")
    {
     // show your message
    }
    else
    {
     //call the base
    }

    Greetings,
    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.
  11. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    09 Dec 2009
    Link to this post
    Hey Ivan - 

        I'm sorry . . . I didn't say that very well.  What I'm trying to ask is why can I add a UserControl by dragging and dropping it on the page and everything works as expected, but when I add it programatically through the API, It doesn't work as expected?  It's not as if i want a different behavior when the page is in edit mode verses when the page is published.

    -Matt
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Dec 2009
    Link to this post
    Hello mimholt,

    Have you tried to use this way

    myPage.Staged.AddControl("ContentPlaceHolder", "~/CustomControls/UserControl.ascx", "UserControl1");

    This should work correctly.


    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.
Register for webinar
12 posts, 0 answered