Web页面,即:.aspx文件
页面的根目录下,分为了5部分
[0]-{System.Web.UI.LiteralControl}
[1]-{System.Web.UI.HtmlControls.HtmlHead}
[2]-{System.Web.UI.LiteralControl}
[3]-{System.Web.UI.HtmlControls.HtmlForm}
[4]-{System.Web.UI.LiteralControl}
内容依次为:
[0]-{System.Web.UI.LiteralControl}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
[1]-{System.Web.UI.HtmlControls.HtmlHead}
<head>
......
</head>
[2]-{System.Web.UI.LiteralControl}
<body>
[3]-{System.Web.UI.HtmlControls.HtmlForm}
<form>
......
</form>
[4]-{System.Web.UI.LiteralControl}
</body>
</html>
所以,在遍历所有页面上的控件的时候,就在this.Controls[3]中找.
问题:遍历Web页面上所有的TextBox,并全部设置为1111
protected void btn_Click(object sender, EventArgs e) { foreach ( System.Web.UI.Control contrl in this.Controls[3].Controls) { if (contrl is System.Web.UI.WebControls.TextBox) { System.Web.UI.WebControls.TextBox txt = (System.Web.UI.WebControls.TextBox)contrl; txt.Text = "l"; } } }
那么遍历WinForm中的控件呢?
private void button1_Click(object sender, EventArgs e) { foreach ( System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.TextBox) { System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)control; txt.Text = "bbbb"; } } }