通过asp.net中的TextBoxes进行迭代 - 为什么这不起作用?

时间:2010-11-03 22:01:38

标签: c# asp.net textbox

我有两种方法,我试图在asp.net页面中遍历我的所有文本框。第一个是工作,但第二个没有返回任何东西。有人可以向我解释为什么第二个不起作用?

这样可行:

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

和“不工作”代码:

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

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

3 个答案:

答案 0 :(得分:9)

您的第一个示例是进行一级递归,因此您将获得控件树中多个控件深的TextBox。第二个示例仅获取顶级TextBox(您可能很少或没有)。

这里的关键是Controls集合不是页面上的每个控件 - 相反,它只是当前控件的直接子控件(和{{1} }是一种Page)。 那些控件可能反过来拥有自己的子控件。要了解有关详情,请参阅ASP.NET Control Tree hereNamingContainers here。要真正获得页面上任何位置的每个TextBox,您需要一个递归方法,如下所示:

Control

用作extension method,如下所示:

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

答案 1 :(得分:2)

你需要递归。控件采用树形结构 - Page.Controls不是页面上所有控件的展平列表。您需要执行以下操作以获取TextBox的所有值:

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 :(得分:0)

你可以尝试这段代码来获取所有TextBox的列表

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