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

Forums / Developing with Sitefinity / Programmatically add and modify a User Control

Programmatically add and modify a User Control

3 posts, 1 answered
  1. Olivier
    Olivier avatar
    57 posts
    Registered:
    09 Jul 2010
    26 Jul 2010
    Link to this post
    Hi,

    I made a User Control very similar to the Links List sample.

    It seems to work fine in administration but I have persistence problems when I'm adding and then modifying it programmatically. The control is added the the page, but it stays with default or empty values...

    Here's my code to add the control; it works:
    private void AddLinksListControl(Telerik.Cms.IPage page)
    {
        Telerik.Cms.ICmsPage pageForEdit = cmsManager.GetPage(page.ID, true) as Telerik.Cms.ICmsPage;
        pageForEdit.Staged.AddControl(contentContainerName, "~/UserControls/Links/LinksListBox.ascx", "linksListBoxControl");
        pageForEdit.Publish();
    }

    Here's my code to set the data of the control, called right after the above method; it does not seem to work.
    var pageForEdit = cmsManager.GetPage(page.ID, true) as Telerik.Cms.ICmsPage;
     
    foreach (ICmsWebControl cmsControl in pageForEdit.Staged.Controls)
    {
        if (cmsControl.TypeName.Equals("~/UserControls/Links/LinksListBox.ascx"))
        {
            ICmsWebControl editedControl = cmsManager.GetControl(cmsControl.ID, true);
            var linksListBox = editedControl.LoadControl() as LinksListBox;
            if (linksListBox != null)
            {                     
                linksListBox.ListLinks = linksDictionary;
                linksListBox.Title = "MY LINKS LIST";
                editedControl.SetControl(linksListBox);
                cmsManager.SaveControl(editedControl);
            }
        }
    }
     
    pageForEdit.Publish();

    ListLinks and Title are both not saved when I check the page after.

    Do you have any idea what is missing ?

    Thank you

    Olivier
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    26 Jul 2010
    Link to this post
    Hi Olivier,

    Call LoadControl(this.Page) and then CheckIn the page.

    sample code


    if (cntrl.IsAscx && cntrl.TypeName == "~/UserControls/Links/LinksListBox.ascx")
        {
            Control c = cntrl.LoadControl(this.Page);
            LinksListBox mycontrol = c as LinksListBox;
            mycontrol.SomeProperty = "value";
            Manager.SaveControl(cntrl);
            ((ICmsPage)Manager.GetPage(page.ID, true)).Staged.CheckIn();
            Nolics.ORMapper.Base.DataConnection.InitWebRequest();
            break;
      
        }


    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. Olivier
    Olivier avatar
    57 posts
    Registered:
    09 Jul 2010
    26 Jul 2010
    Link to this post
    Hi Ivan,

    Thanks for the very fast answer !

    I changed my code as you mentioned and it's now working.

    Thank you

    Olivier
Register for webinar
3 posts, 1 answered