Iterating through TextBoxes in asp.net – why is this not working?

I have 2 methods I tried to iterate through all my textboxes in an asp.net page. The first is working, but the second one is not returning anything. Could someone please explain to me why the second one is not working?

This works ok:

List<string> list = new List<string>();

    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
                list.Add(((TextBox)childc).Text);
            }
        }
    }

and the “not working” code:

List<string> list = new List<string>();

    foreach (Control control in Controls)
    {
        TextBox textBox = control as TextBox;
        if (textBox != null)
        {
            list.Add(textBox.Text);
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your first example is doing one level of recursion, so you’re getting TextBoxes that are more than one control deep in the control tree. The second example only gets top-level TextBoxes (which you likely have few or none).

The key here is that the Controls collection is not every control on the page – rather, it is only the immediate child controls of the current control (and a Page is a type of Control). Those controls may in turn have child controls of their own. To learn more about this, read about the ASP.NET Control Tree here and about NamingContainers here. To truly get every TextBox anywhere on the page, you need a recursive method, like this:

public static IEnumerable<T> FindControls<T>(this Control control, bool recurse) where T : Control
{
    List<T> found = new List<T>();
    Action<Control> search = null;
    search = ctrl =>
        {
            foreach (Control child in ctrl.Controls)
            {
                if (typeof(T).IsAssignableFrom(child.GetType()))
                {
                    found.Add((T)child);
                }
                if (recurse)
                {
                    search(child);
                }
            }
        };
    search(control);
    return found;
}

Which is used as an extension method, like so:

var allTextBoxes = this.Page.FindControls<TextBox>(true);

Method 2

You need to recurse. The controls are in a tree structure – Page.Controls is not a flattened list of all controls on the page. You’d need to do something like the following to get all values of TextBoxes:

void GetTextBoxValues(Control c, List<string> strings)
{
    TextBox t = c as TextBox;
    if (t != null)
        strings.Add(t.Text);

    foreach(Control child in c.Controls)
        GetTextBoxValues(child, strings);
}

List<string> strings = new List<string>();
GetTextBoxValues(Page, strings);

Method 3

you can try this piece of code to get list of all TextBoxes

public partial class _Default : System.Web.UI.Page
   {

       public List<TextBox> ListOfTextBoxes = new List<TextBox>();
       protected void Page_Load(object sender, EventArgs e)
       {
           // after execution this line
           FindTextBoxes(Page, ListOfTextBoxes);
           //ListOfTextBoxes will be populated with all text boxes with in the page.

       }


       private void FindTextBoxes(Control Parent, List<TextBox> ListOfTextBoxes)
       {
           foreach (Control c in Parent.Controls) {
               // if c is a parent control like panel
               if (c.HasControls())
               {
                   // search all control inside the panel
                   FindTextBoxes(c, ListOfTextBoxes);
               }
               else {
                   if (c is TextBox)
                   {
                       // if c is type of textbox then put it into the list
                       ListOfTextBoxes.Add(c as TextBox);
                   }
               }
           }
       }
   }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x