Problem with dynamically adding controls

6 posts, 0 answers
  1. Laurent
    Laurent avatar
    4 posts
    Member since:
    Feb 2012

    Posted 23 Feb 2012 Link to this post

    Hello,

    I'm having a little issue with the RadDock controls.
    What I am trying to do is add a WebPart zone in the ContentContainer of my dock. This first step works fine.

    EDIT : It works for the raddock which have their states saved in DB and are thus created on the 
    dockLayoutHome_LoadDockLayout event.
    
    For the docks that I try to add on "runtime" (I have a RadAjaxPanel which contains the RadDockLayout) I get the following error:
    
    System.ApplicationException: A program is attempting to add a new Web Part Zone to this page. Web Part Zones must be defined at the time a Web Part Page is created.

    Any help would be appreciated :)



    Then I want to add a WebPart in the previously created WebPartZone. This step also works fine.

    The problem is when a postback occurs on the page.
    My docks contains the WebPart initially created, and a new one! At every postback, the dock contains a new WebPart...

    Here is the code that adds the WebPart:
    public class CustomRadDock : RadDock
        {
            #region Overrides
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                this.EnsureChildControls();
            }
     
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
     
                WebPartZone wpZone = new WebPartZone();
                wpZone.ID = String.Format("{0}_webPartZone", this.ID);
                wpZone.PartChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
     
                this.ContentContainer.Controls.Add(wpZone);
                LoadWebPart("myname");
            }
            #endregion Overrides
     
            #region Methods
            private void LoadWebPart(String webPartName_)
            {
                _log.Info("Adding WebPart to dock");
     
                using (SPSite site = new SPSite("mysite"))
                {
                    SPWeb web = site.RootWeb;
                    SPFile page = web.GetFile("mypage");
     
                    web.AllowUnsafeUpdates = true;
                    web.Update();
     
                    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                    {
                        XmlElement p = new XmlDocument().CreateElement("p");
                        p.InnerText = "Hello World";
                        ContentEditorWebPart cewp = new ContentEditorWebPart();
                        cewp.Content = p;
                        wpmgr.AddWebPart(cewp, String.Format("{0}_webPartZone", this.ID), 0);
                        wpmgr.SaveChanges(cewp);
                    }
     
                    web.AllowUnsafeUpdates = false;
                    web.Update();
                }
            }
            #endregion Methods

    Let me know if you need further information.
    And if you have any solution of course!
  2. Slav
    Admin
    Slav avatar
    1355 posts

    Posted 27 Feb 2012 Link to this post

    Hi Laurent,

    There should not be a problem in using the RadDock as a container for a WebPartZone and a WebPart. Note that due to the way SharePoint functions, it will not be possible to dynamically create a dock control that contains a WebPartZone. This is why an error occurs when you try to add RadDocks dynamically.

    As for creating new WebParts on every postback, this behavior is not related to the RadDock control. In this case the RadDock controls is used only as a container and the encountered behavior can be examined if you create a standard container for the WebParts.

    Greetings,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Laurent
    Laurent avatar
    4 posts
    Member since:
    Feb 2012

    Posted 28 Feb 2012 Link to this post

    Thank you for your answer.

    In fact, I found out that the postback problem had nothing to do with the Dock control, but with the way I was adding WebParts on my page.

    But I disagree about the point that it is not possible to dynamically create a dock with a WebPartZone in it. In fact, I did it! It is a bit tricky, but it works :)


    Greetings.
  5. Slav
    Admin
    Slav avatar
    1355 posts

    Posted 01 Mar 2012 Link to this post

    Hello Laurent,

    I am glad that you have managed to resolve your problem. Indeed, dynamically creating RadDock that contains WebPartZone is possible, as you have already found out. Please, excuse me for misleading you.

    All the best,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Yankev
    Yankev avatar
    3 posts
    Member since:
    Jun 2012

    Posted 12 Sep 2012 Link to this post

    I'm trying to do the same, what was the solution that was found? Also I'm trying to dynamically set connections for the webparts and I'm running into an error that the webpart doesn't belong to the webpart collection. Any ideas?
  7. SLM
    SLM avatar
    13 posts
    Member since:
    Oct 2011

    Posted 13 Sep 2012 Link to this post

    Hello!

    I guess you are retrieving the webpartmanager correctly already?
    If not, you have to.

    See below :
    web.AllowUnsafeUpdates = true;
      
                    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                    {
                        XmlElement p = new XmlDocument().CreateElement("p");
                        p.InnerText = "Hello World";
                        ContentEditorWebPart cewp = new ContentEditorWebPart();
                        cewp.Content = p;
                        wpmgr.AddWebPart(cewp, String.Format("{0}_webPartZone", this.ID), 0);
                        wpmgr.SaveChanges(cewp);
                    }
      
                    web.AllowUnsafeUpdates = false;

    You have to know that dynamically adding controls (or handling them) should be done at oninit event.

    Hope this helps.
    Greetings.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017