2010-11-03 60 views
7

我有2个方法我尝试遍历所有我的文本框在一个asp.net页面。第一个工作,但第二个没有返回任何东西。有人可以向我解释为什么第二个不工作?在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); 
     } 
    } 
+0

在第二批代码中,控件是否包含任何东西? – brumScouse 2010-11-03 22:05:44

回答

9

你的第一个例子是做递归的一个水平,所以你得到超过文本框在控制树中深入一个控制。第二个示例仅获取顶级TextBoxes(您可能很少或没有)。

的这里关键是Controls收集不是每个页面上的控制 - 更确切地说,它只是直接子控制电流控制的(和Page是一种类型的Control)。 那些控件可能依次拥有自己的子控件。要详细了解此信息,请阅读ASP.NET Control Tree here和约NamingContainers here。要真正让每一个文本框的页面上的任何地方,你需要一个递归方法,像这样:

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

被用作一个extension method,就像这样:

var allTextBoxes = this.Page.FindControls<TextBox>(true); 
+0

好吧,但是顶级TextBoxes是什么意思?我的文本框只是“普通”文本框的形式.. – 2010-11-03 22:05:12

+0

@Svein看到我更新的答案。您的Web表单是一个控件树 - 包含包含控件的控件的控件等等。 “顶级”意味着生活在层次最顶层或最外层的控制。 – 2010-11-03 22:10:47

+0

非常感谢您的详细解释和代码示例!我喜欢Stackoverflow,那么多想要帮助其他人的伟大人物:-) – 2010-11-03 22:15:24

1

你需要递归。这些控件是树形结构 - Page.Controls不是页面上所有控件的展开列表。你需要做类似下面的东西拿到文本框的所有值:

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

你可以试试这段代码来获取所有文本框

名单
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

我之前已通知您关于链接到您自己的网站而没有署名。 – 2013-03-05 06:46:53