遍历页面中的所有textbox设为空
2010-09-01 16:52
337 查看
今天一同学问我怎么样可以遍历页面中的textbox ,我一直记得是页面有个什么控件的结合,直接遍历一下就可以获得,就直接告诉他了。
后来想想还是自己做一遍比较好,发现了点小问题,直接用for循环怎么也遍历不到。最后调试才发现,一个for循环只能遍历最外边一层的控件,要想全部获得就要使用递归,贴出测试代码:
protected void Button1_Click(object sender, EventArgs e)
{
ClearControl(this.Controls);
}
protected void ClearControl(ControlCollection ct)
{
foreach (Control ctl in ct)
{
if (ctl is TextBox)
{
TextBox t = (TextBox)ctl;
t.Text = string.Empty;
}
if (ctl.HasControls())
{
ClearControl(ctl.Controls);
}
}
}
上边是.net里面的,顺便把winform中的遍历控件的方法也贴出来,防止以后再忘了:
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
后来想想还是自己做一遍比较好,发现了点小问题,直接用for循环怎么也遍历不到。最后调试才发现,一个for循环只能遍历最外边一层的控件,要想全部获得就要使用递归,贴出测试代码:
protected void Button1_Click(object sender, EventArgs e)
{
ClearControl(this.Controls);
}
protected void ClearControl(ControlCollection ct)
{
foreach (Control ctl in ct)
{
if (ctl is TextBox)
{
TextBox t = (TextBox)ctl;
t.Text = string.Empty;
}
if (ctl.HasControls())
{
ClearControl(ctl.Controls);
}
}
}
上边是.net里面的,顺便把winform中的遍历控件的方法也贴出来,防止以后再忘了:
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
相关文章推荐
- 遍历页面中所有的TextBox,并将值设置成String.Empty
- C# 面试题解析-请遍历页面上所有的TextBox控件并给它赋值为string.Empty
- C# 面试题解析-请遍历页面上所有的TextBox控件并给它赋值为string.Empty
- 遍历页面所有TextBox控件并给它赋值为string.Empty
- 遍历页面所有Textbox控件并给他赋值为string.empty
- 遍历页面上所有TextBox,并赋值为String.Empty(Asp.Net和WinForm)
- 遍历页面中所有的TextBox,并将值设置成String.Empty
- 如何用foreach遍历页面上所有的TextBox(转载)
- 遍历清空页面所有Textbox
- 遍历页面中所有的服务器控件TextBox
- 如何用foreach遍历页面上所有的TextBox(转载)
- 遍历页面上所有的TextBox
- 如何用foreach遍历页面上所有的TextBox,判断他们是否为空?
- 遍历页面所有TextBox控件并给它赋值为string.Empty
- 遍历页面中所有的TextBox
- 遍历页面上所有的TextBox控件并给它赋值为string.Empty
- 遍历页面上所有TextBox,并赋值为String.Empty
- 遍历页面上所有TextBox,并赋值为String.Empty
- 遍历页面上所有TextBox控件
- asp.net(C#)遍历页面上所有的TextBox控件并给它赋值