First thing. Make sure you are not creating your controls inside an if (!isPostBack){}
- as they need recreating on each postback.
Secondly, I don't believe your loop will find all controls as it will only really travel through the first level.
Ideally, you should search for the controls recursivley.
Here is a recursive method that I use - this will help find all controls of a given ID.
/// <summary>
/// Finds a Control recursively. Note finds the first match that exists
/// </summary>
/// <param name="ContainerCtl">Should be the lowest container in the heirarchy, for eg dont choose Master page if you can pick the specific panel</param>
/// <param name="IdToFind">ID of the control you are looking for</param>
/// <returns>the control if found else null</returns>
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id) { return Root; }
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null) { return FoundCtl; }
}
return null;
}
Now, what I would do is:
When you create your TextBox
's, store all the ID
's in an Array
. Then when you need to access them, loop through the Array
and for each entry, call the above method. this will then return the TextBox
you need.