[CLOSED] Child.AddTo or Parent.Items.Add

  1. #1

    [CLOSED] Child.AddTo or Parent.Items.Add

    Hello,

    This is a general question. When building dynamic controls, ParentComponent.Items.Add(ChildComponent) works during page load but not working when called from direct event. ChildComponent.AddTo(ParentComponent) works for both. Is there a best practice or pros/cons for deciding between two alternatives?

    Note-1: All in code behind.
    Note-2: ParentComponent is defined in the markup, ChildComponent's are dynamically created and added to parent from code behind.

    I am asking this to decide the right strategy for refreshing those dynamic child components (tree panel menu etc.)

    Thanks.
    Last edited by Daniil; May 23, 2013 at 9:58 AM. Reason: [CLOSED]
  2. #2
    Hello!

    I think using
    ChildComponent.AddTo(ParentComponent)
    is more appropriate for most cases.

    I'll ask my colleagues advice you too.
  3. #3
    Hello,

    The AddTo method and other "render" methods Render, InsertTo generate JavaScript code which is excessive for initial page loading. Moreover, on some circumstances those generated scripts can cause a JavaScript error. So, these methods should be used during an AJAX request only (DirectEvent/DirectMethod).

    During initial page loading please just populate a container's Items collection.

    Commonly, I prefer the following template.
    container.Items.Add(control);
    
    if (X.IsAjaxRequest)
    {
        control.Render();
    }
  4. #4
    That was very informative. I understood the difference well ( I think I did (; ).

    Are there any practical reasons to select between two strategies below?

    Thanks.

    1:
    if (X.IsAjaxRequest)
    {
        control.AddTo(container);
    }
    else
    {
        container.Items.Add(control);
    }
    2:
    container.Items.Add(control);
     
    if (X.IsAjaxRequest)
    {
        control.Render();
    }
    Last edited by bayoglu; May 23, 2013 at 8:47 AM.
  5. #5
    Those approaches should be the same, just use any you wish.

    The second one is 2 code lines less;)
  6. #6
    Sure it is (;

    Thanks Daniil, please mark as closed.
  7. #7
    Please note that if you want to handle DirectEvents of dynamically created controls, you will have to recreate those controls.
    http://forums.ext.net/showthread.php?14316#post60155

Similar Threads

  1. [CLOSED] Parent Child solution
    By otouri in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: May 17, 2013, 1:49 PM
  2. [CLOSED] [#144] GridPanel - Parent/Child
    By adelaney in forum 2.x Legacy Premium Help
    Replies: 9
    Last Post: Feb 11, 2013, 8:58 PM
  3. Replies: 1
    Last Post: May 29, 2012, 8:51 AM
  4. Treepanel - get child values of selected parent
    By Tbaseflug in forum 1.x Help
    Replies: 2
    Last Post: Nov 10, 2009, 11:28 AM
  5. display issue on Parent/Child window
    By egodoy in forum 1.x Help
    Replies: 1
    Last Post: Mar 24, 2009, 5:02 PM

Posting Permissions