Iterating through TextBoxes in asp.net - why doesn't it work?

I have 2 methods that I tried to iterate over all my text fields on an asp.net page. The first works, but the second does not return anything. Can someone explain to me why the second is not working?

This works fine:

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 code doesnโ€™t work:

 List<string> list = new List<string>(); foreach (Control control in Controls) { TextBox textBox = control as TextBox; if (textBox != null) { list.Add(textBox.Text); } } 
+7
c # textbox
source share
3 answers

Your first example runs one recursion level, so you get TextBoxes that contain more than one control in the control tree. In the second example, only top-level text fields (which are probably few or not) are received.

The key point 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 Page is the type of Control ). These controls, in turn, may have their own control elements. To learn more about this, read about the ASP.NET Management Tree here and NamingContainers here . To really get every text block anywhere on the page, you need a recursive method, for example:

 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; } 

Used as an extension method , for example:

 var allTextBoxes = this.Page.FindControls<TextBox>(true); 
+9
source share

You need to unsubscribe. The controls are in a tree structure - Page.Controls not a smoothed list of all the controls on the page. To get all the values โ€‹โ€‹of TextBoxes, you need to do something like the following:

 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); 
+2
source share

you can try this piece of code to get a list of all text fields

 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); } } } } } 
0
source share

All Articles