Search code examples
asp.netuser-controlsupdatepanelcontrolcollection

Adding Controls to Control Collection from an update panel


Following these two threads: How can I create an Array of Controls in C#.NET? Cannot Access the Controls inside an UpdatePanel

I current have this:

ControlCollection[] currentControlsInUpdatePanel = new ControlCollection[upForm.Controls.Count];
foreach (Control ctl in ((UpdatePanel)upForm).ContentTemplateContainer.Controls)
{
    currentControlsInUpdatePanel.
}

currentControlsInUpdatePanel does not have an add or insert method. why does the first link i post allow that user to .add to his collection. This is what I want to do, find all the controls in my upForm update panel. but i dont see how i can add it to my collection of controls.


Solution

  • I don't think this code makes sense. You are creating an array of ControlCollection objects and trying to store Control objects in it. Furthermore, since currentControlsInUpdatePanel object is an array, there will not be an Add() method available on that object.

    If you want to use the Add() method, try creating currentControlsInUpdatePanel as a List object.

    Example:

    List<Control> currentControlsInUpdatePanel = new List<Control>();
    foreach(Control ctl in ((UpdatePanel)upForm).ContentTemplateContainer.Controls)
    {
        currentControlsInUpdatePanel.Add(ctl);
    }
    

    If you want to continue to use an array to store the Control objects, you will need to use the index value to set your objects in the array.

    Example:

    Control[] currentControlsInUpdatePanel = new Control[((UpdatePanel)upForm).ContentTemplateContainer.Controls.Count];
    for(int i = 0; i < upForm.Controls.Count; i++)
    {
        currentControlsInUpdatePanel[i] = ((UpdatePanel)upForm).ContentTemplateContainer.Controls[i];
    }