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

Forums / General Discussions / AddControl UserControl

AddControl UserControl

5 posts, 0 answered
  1. Jaime Weise
    Jaime Weise avatar
    120 posts
    Registered:
    02 Nov 2008
    12 Aug 2010
    Link to this post
    Add Custom UserControls Registered in the Toolbox using Code
    I have registered my custom control in the web configuration file. I can drag and drop it nicely in the backend, everything is groovy. Now, my requirements are that I create multiple instances of this controlusing code and add it to a page template on a page of my choice. Can someone guide me to how I would add custom user controls using code followed by adding property values for the instances of each of the added controls?

    I was able to load the control into the page with the following control. It even works at runtime.

    // create a new instance of CmsManager
    Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
     
    Control ctrol1 = Page.LoadControl("~/UserControls/Link.ascx");
     
    // add this control
    Telerik.Cms.Web.CmsSiteMapNode currentPage = (Telerik.Cms.Web.CmsSiteMapNode)SiteMap.Provider.CurrentNode;
     
    // to be able to add a control to a page we need a instance of page associated with a transaction
    Telerik.Cms.ICmsPage cmsPage = cmsManager.GetPage(currentPage.CmsPage.ID, true) as Telerik.Cms.ICmsPage;
     
    cmsPage.Staged.AddControl("ContentPlaceHolderMain", ctrol1);
     
    cmsPage.Publish();

     The only problem is that I can't see the control properly at design time. I receive an error inside of the usercontrol in design time mode.

    Index was outside the bounds of the array.

    What is missing in the above code that is creating this particular error to occur?

    -- 
    Jaime


  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Aug 2010
    Link to this post
    Hi jaime,

    1. To set the control properties you need to get the page again, get its staged version and controls collection. Then, find your control as ICmsWebControl and then cast it to type of your user control. You can find some samples here.

    2.This type of error "Index was outside the bounds of the array" is  thrown by the .NET Framework, when you are accessing an item in an indexed object and the accessed index is out of bounds .You have an ArrayList and you are trying to access an index that is not available. It looks like at the time your are trying to retrieve the data the control has no items. The problem is most probably related to the way you are binding this control and in which stage of its lifecycle you are doing this. Note that in Edit mode the Page is null, so this could be one of the reasons for this issue. You could check if the page is null where you bind your data, so that you will escape the page edit mode and the error will not be thrown.

    if(HttpContext.Current.Request.QueryString[ "cmspagemode"] == "edit")
     {
          
     }


    Kind regards,
    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
  3. Jaime Weise
    Jaime Weise avatar
    120 posts
    Registered:
    02 Nov 2008
    13 Aug 2010
    Link to this post
    There is nothing special about the control databinding. I works fine if I drag and drop the control onto the surface as intended by regular user controls. It must be something about the way that I am adding the control to the page. 

    Does the code for adding a custom usercontrol look correct? It seems to me that there would be something that I am  missing. I have tried multiple usercontrols but they all get the error as I explained. Do you know what library throws that particular error? If I can step into there maybe I can isolate the problem.

    As for the properties, I can simple cast to an interface and set the properties or use reflection. I will take a look at the link you provided as well. 
    Thanks,
    Jaime


  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Aug 2010
    Link to this post
    Hi jaime,

    Try calling cmsManager.SavePage();

    Sincerely yours,
    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
  5. Jaime Weise
    Jaime Weise avatar
    120 posts
    Registered:
    02 Nov 2008
    14 Aug 2010
    Link to this post
    I tried that but it didn't work.
    I did however find using a different overload of the AddControl method to be a success. 

    cmsPage.Staged.AddControl("ContentPlaceHolderMain", "~/UserControls/Link.ascx", 0, "MyControl1");


Register for webinar
5 posts, 0 answered