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

后端 未结 3 909
野趣味
野趣味 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 strings)
    {
        TextBox t = c as TextBox;
        if (t != null)
            strings.Add(t.Text);
    
        foreach(Control child in c.Controls)
            GetTextBoxValues(child, strings);
    }
    

    ...

    List strings = new List();
    GetTextBoxValues(Page, strings);
    

提交回复
热议问题