1.遍歷頁面中所有的TextBox,並將值設置成String.Empty
復制代碼 代碼如下:
for (int j = 0; j < this.Controls.Count; j++)
{
foreach (object o in Page.Controls[j].Controls)
{
if (o is TextBox)
{
TextBox txt = (System.Web.UI.WebControls.TextBox)o;
txt.Text = String.Empty;
}
}
}
2.遞歸遍歷
復制代碼 代碼如下:
private void FindAllTextBoxByPageControl(ControlCollection controlCollection)
{
for (int i = 0; i < controlCollection.Count; i++)
{
if (controlCollection[i].GetType() == typeof(TextBox)) //System.Web.UI.WebControls.TextBox
{
(controlCollection[i] as TextBox).Text = String.Empty;
}
if (controlCollection[i].HasControls())
{
//遞歸 (重要) 否則將退出程序
FindAllTextBoxByPageControl(controlCollection[i].Controls);
}
}
}
調用方法
復制代碼 代碼如下:
FindAllTextBoxByPageControl(Page.Controls);