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

Forums / Sitefinity SDK / Programmatically adding User Controls

Programmatically adding User Controls

2 posts, 0 answered
  1. Haydn
    Haydn avatar
    13 posts
    Registered:
    13 May 2008
    11 Apr 2011
    Link to this post
    Hi all,

    I am having some problems adding user controls to pages in Sitefinity 4.0. Below is a list of things im trying to achieve.

    1) To create a page with the Sitefinity API.
    2) To add custom user controls to this page.
    3) To set custom properties of each control on this page.

    I'm working from the following documentation.

    http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-pages-adding-and-removing-controls.html

    Running as a Web Application

    I started with a basic web application, I have been able to add user controls using the following code.

    PageDraftControl draftControl = manager.CreateControl<PageDraftControl>("~/controls/mycontrol.ascx", "holder");
     
    draftPage.Controls.Add(draftControl);

    but i have been unable to work out how to set the properties on this control. so i have tried the following.

    dynamic mycontrol = LoadControl("~/controls/mycontrol.ascx");
     
    mycontrol.Message = "Hello World";
     
    PageDraftControl draftControl = manager.CreateControl<PageDraftControl>(mycontrol, "holder");
     
    draftPage.Controls.Add(draftControl);

    This results in Sitefinity not being able to resolve the type "ASP.project_mycontrol_ascx". At which point i have converted my project to a ASP.NET Web project.

    Running as a Web Project

    Running as a web project (and compiling the project with the type MyControl), i have tried the following.

    MyControl mycontrol = new  MyControl();
     
    mycontrol.Message = "Hello World";
     
    PageDraftControl draftControl = manager.CreateControl<PageDraftControl>(mycontrol, "holder");
     
    draftPage.Controls.Add(draftControl);

    At which point, Sitefinity is now able to resolve the user control type and also set the property value for "Message", but none of the ASP.NET web controls within the user control are initialized, and are all null.

    Note: Other controls added to pages appear to be fine when dragging them to pages from the Sitefinity page editor.

    I have a couple of questions related to this.

    1) Is there a way to update the properties of the user controls once they have been added to a page?

    2) Is there anything that would be preventing the ASP.NET web controls inside the user control from being initialized? Is there something i need to do to ensure they get initialized?

    3) Any sample code would be great :)

    Many Thanks

    Haydn
  2. Haydn
    Haydn avatar
    13 posts
    Registered:
    13 May 2008
    12 Apr 2011
    Link to this post
    In terms of being able to programmatically set properties on User Controls when adding user controls to pages, I have been able to find a work around to this problem using the following code. I hope others find this useful.

    this is in reference to the following documentation.

    http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-pages-adding-and-removing-controls.html

    dynamic usercontrol = BuildManager.CreateInstanceFromVirtualPath("~/mycontrol.ascx", typeof(UserControl));
     
    usercontrol.Message = "Hello World"; // Some public property on this user control.
     
    PageDraftControl pageDraftControl = pageManager.CreateControl<PageDraftControl>();
     
    pageDraftControl.ObjectType = "~/mycontrol.ascx";
     
    pageDraftControl.PlaceHolder = "ContentPlaceHolder1";
     
    pageManager.ReadProperties(usercontrol, pageDraftControl); // Voodoo here
     
    draftPage.Controls.Add(pageDraftControl);

    ...

    however, I have not been able to find a way to update the properties once they have been added to the page.

    Open to suggestions
2 posts, 0 answered