Iterating through text fields with asp.net

I am creating a page with asp.net. I have a form with a table containing TextBoxes and a submit button. When the form is submitted, I want to capture all the text that has been entered into the TextBoxes and work with them. For this, I have the following method:

protected void Button1_Click(object sender, EventArgs e) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (Control c in this.Controls) { if (c.GetType().Name == "TextBox") { TextBox tb = (TextBox)c; sb.AppendLine(tb.Text); } } Label1.Text = sb.ToString(); } 

The problem is that the controls do not seem to contain any of my text fields. When I iterate over the controls and print out their names, the only thing I get is "site_master". (I also tried Controls and Page.Controls instead .Controls).

Is there something wrong with my iterator? Is there any other way in which I could iterate over all the text fields in a table or on a page? What is the best way to do this?

+4
source share
3 answers

Would it be too difficult to build a List<Textbox> if you know all of your text field controls?

 List<Textbox> txtBoxes = new List<Textbox>(); txtBoxes.Add(tb1); txtBoxes.Add(tb2); //etc.. 

Then you have a good list to work with

+4
source

If I knew that all controls are contained in this control, I would simply poll the controls for this control. For example, this.Form.Controls . However, if they can be nested in other child controls, you can recursively examine the depths from a common external container.

 private IEnumerable<T> FindControls<T>(Control parent) where T : Control { foreach (Control control in parent.Controls) { if (control is T) yield return (T)control; foreach (T item in FindControls<T>(control)) yield return item; } } 

Thus, this will allow you to get all TextBox children.

 List<TextBox> textBoxes = this.FindControls<TextBox>(this).ToList(); string output = string.Join(",", textBoxes.Select(tb => tb.Text)); 
+1
source

I am going to assume that you are using ASP.NET web forms. Usually you declare your controls on an aspx page using something similar to

 <asp:TextBox ID="someId" runat="server/> 

If you have done this, then your code should only have a reference to the someId variable and the Text property in order to get / set the text in the control.

If you dynamically create controls on the server, you can insert them into the list and go through it. Make sure you create controls and add them to the table during the correct part of the page life cycle . When you add them to a cell in a table, you can also save the link to the control in the list and simply list the list in the event handler.

Maybe something like strings (I did not compile this, so there are probably problems):

 public class MyPage: Page { private List<TextBox> TxtBoxes = new List<TextBox>(); //registered for the preinit on the page.... public void PreInitHandler(object sender, EventArgs e) { for(var i = 0; i < 2; i++) { var txtBox = new TextBox{Id = textBox+i}; //...add cell to table and add txtBox Control TxtBoxes.Add(txtBox); } } } 
0
source

All Articles