我有两种方法试图迭代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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和"不工作"代码:
List<string> list = new List<string>();
foreach (Control control in Controls)
{
TextBox textBox = control as TextBox;
if (textBox != null)
{
list.Add(textBox.Text);
}
}
Run Code Online (Sandbox Code Playgroud)