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

后端 未结 3 891
野趣味
野趣味 2021-01-05 10:12

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 exp

相关标签:
3条回答
  • 2021-01-05 10:44

    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);
    
    0 讨论(0)
  • 2021-01-05 10:52

    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);
    
    0 讨论(0)
  • 2021-01-05 11:05

    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);
                       }
                   }
               }
           }
       }
    
    0 讨论(0)
提交回复
热议问题