【发布时间】:2023-03-20 01:43:01
【问题描述】:
我有 2 种方法尝试遍历我在 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);
}
}
【问题讨论】:
-
在第二批代码中,控件是否包含任何内容?