//1.遍歷頁面中所有的TextBox,并將值設(shè)置成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); } } } //調(diào)用方法 FindAllTextBoxByPageControl(Page.Controls); |
|
來自: 悟靜 > 《.net和asp.net》