如何遍历页面所有控件?
包括web控件跟html控件
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
int j = 0;
for (int i = 0;i < Form1.Controls.Count;i++){
j++;
}
}
我在for循环上设断点,为何Form1.Controls.Count等于7呢?页面上我总共就只有三个按钮控件
问题点数:20、回复次数:2Top
1 楼appleren(红心儿萝卜)回复于 2005-08-29 16:28:56 得分 20
我也遇到过这个问题,我用以下代码进行遍历的
private void loopControl( ref string result, Control con )
{
if( !con.HasControls() )
return;
else
{
foreach( Control wc in con.Controls )
{
if( wc.ID != null )
result += wc.ID.ToString() + " dd ";
loopControl( ref result, wc );
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string result="";
loopControl( ref result, this );
}
如果,没有加那个if(wc.ID != null )的判断,也会多出来好几个控件。具体机理没有进一步探究了。而且,那些控件是没有Id的。可能自己生成的吧。Top
2 楼wtiancai(博学,审问,慎思,明辨,笃行.)回复于 2005-08-29 16:35:25 得分 0
visual studio.net,html设计中如果各个控件中有空格,它就会多出控件出来,奇怪Top




