foreach (System.Web.UI.Control control in Controls)
{
if (control is TextBox)
{
TextBox box = (TextBox)control;
box.Visible = true;
}
if(control is Label)
{
Label lab = (Label)control;
lab.Visible = false;
}
}
当 control is textbox时 不认 control不认textbox 从而不进入if里
{
if (control is TextBox)
{
TextBox box = (TextBox)control;
box.Visible = true;
}
if(control is Label)
{
Label lab = (Label)control;
lab.Visible = false;
}
}
当 control is textbox时 不认 control不认textbox 从而不进入if里
foreach(Control c in this.Forms.Controls)
{
if(c is TextBox)
//do something
else if (c is Label)
//do something
}
/// <summary>
/// 递归遍历页面的所有控件
/// </summary>
/// <param name="c"></param>
static private void Recursion(Control c)
{ foreach (Control C in c.Controls)
{ switch (C.GetType().Name)
{
//PageLoad事件执行过以后,用户控件的Load事件才执行
//所以不对用户控件处理
case "UserControl":
continue;
case "Repeater":
break;
case "DataList":
break;
case "GridView":
break;
} Recursion(C);
}
#endregion
web中也有Form的
我那段代码遍历的是Form1的控件,所以我问你要遍历的是哪个容器里的?
LZ直接遍历Controls,只能遍历到form1(默认为form1)
你的TextBox应该在form1里吧
所以可以直接遍历form1里 的控件
foreach(Control ctr in form1.Controls)
public void SearchControl(ControlCollection controls)
{
foreach (System.Web.UI.Control control in controls)
{
if (control.Controls != null && control.Controls.Count > 0)
{
SearchControl(control.Controls);
return;
} if (control is TextBox)
{
TextBox box = (TextBox)control;
box.Visible = true;
}
if (control is Label)
{
Label lab = (Label)control;
lab.Visible = false;
}
}
}
if (control is TextBox) //没这么智能吧,这样写也能认识?