Howto add dynamic created controls to AjaxSettings?

3 posts, 1 answers
  1. JP
    JP avatar
    122 posts
    Member since:
    Aug 2011

    Posted 01 Dec 2011 Link to this post

    Hi,

    I have a page containing a ribbon bar and a content control (div).
    I added AjaxSettings for this (in CreateChildControls):
    AjaxSetting ajaxSetting1 = new AjaxSetting(m_ribbonBar.ID);
    ajaxSetting1.UpdatedControls.Add(new AjaxUpdatedControl(m_contentPanel.ID, m_loadingPanel.ID) { UpdatePanelHeight = new Unit("100%") });
    RadAjaxManager1.AjaxSettings.Add(ajaxSetting1);

    The control for the content div is dynamically created (dependent on the button I clicked in the ribbon bar).
    So far, this works. While the control is dynamically loaded and added to the content div, the content div shows the loading panel.

    Now I built a control containing a radgrid and a button for testing purposes:
    protected override void CreateChildControls()
    {
             base.CreateChildControls();
     
             Button b = new Button();
             b.ID = "B";
             b.Text = "Search again";
             b.Click += new EventHandler(b_Click);
             Controls.Add(b);
             m_grid = new RadGrid();
             m_grid.ID = "Grid";
             m_grid.NeedDataSource += (grid_NeedDataSource);
             m_grid.AllowPaging = true;
             m_grid.PageSize = 10;
             Controls.Add(m_grid);
             RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(b, m_grid, LoadingPanel);
             RadAjaxManager.GetCurrent(Page).AjaxSettings.AddAjaxSetting(m_grid, m_grid, LoadingPanel);
    }

    The loading panel is the same as the one from the main page (if I don't use a loading panel here, I get the error too).
    When the control is dynamically created the first time, everything is ok. The div shows the loading panel until the control is ready. When I click the button, m_radGrid.Rebind() is called and the RadGrid shows a loading panel.
    But when another dynamically created control is added to the content div (I call m_contentPanel.Clear() first), I get a loading panel and this exception (right after I added the grid to the ajaxsettings):

    Exception type: InvalidOperationException 
        Exception message: Collection was modified; enumeration operation may not execute
       at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
       at Telerik.Web.UI.RadAjaxControl.OnPagePreRender(Object sender, EventArgs e)
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.UI.Control.OnPreRender(EventArgs e)
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    What is the correct way to add dynamically created controls to the AjaxSettings so that my case will work?

    Thanks!
  2. Answer
    Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 06 Dec 2011 Link to this post

    Hi Jp,

    Try adding the ajax settings on Load or PreRender instead of using the CreateChildControls for this purpose and see if it makes any difference.

    Regards,
    Iana Tsolova
    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. JP
    JP avatar
    122 posts
    Member since:
    Aug 2011

    Posted 06 Dec 2011 Link to this post

    Thanks, that works perfectly. The best way I found is to call EnsureChildControls in the base class OnLoad event.
Back to Top