foreach (Control aa in this.Form.Controls){ if (aa.GetType().ToString() == "System.Web.UI.WebControls.TextBox") { ((TextBox)aa).Text = string.Empty; }}
foreach (Control c in groupBox1.Controls) { if (c is TextBox) { //这里写代码逻辑 } } 遍历的时候,需要用Control遍历: 如果直接使用foreach(TextBox t in groupBox1.Controls) 并且groupbox上有Lable或其他非textbox控件的时候,会提示不能强制转换为textbox 所以,需要使用 is来判断,获取的控件是否为textbox
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 ; } }
//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;