遍历页面上所有的控件,判断其实不是TextBox,是的话就将其值赋为空。
方法一:
代码
private void Clear()
{
foreach (Control control in this.FindControl("form1").Controls)
{
if (control is TextBox)
{
((TextBox)control).Text = string.Empty;
// Clear(control.Controls);
}
}
}
方法二:
代码
private void Clear2()
{
Type t = typeof(TextBox);
FieldInfo[] infos =this.GetType().GetFields(BindingFlags.GetField|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance);
for (int i = 0; i < infos.Length; i++)
{
if (infos[i].FieldType.Name == t.Name)
{
((TextBox)infos[i].GetValue(this)).Text = string.Empty;
}
}
}