欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net下遍历页面中所有的指定控件的代码

程序员文章站 2024-03-08 22:23:46
1.遍历页面中所有的textbox,并将值设置成string.empty 复制代码 代码如下: for (int j = 0; j < this.controls.c...
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;
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);
}
}
}

调用方法
复制代码 代码如下:

findalltextboxbypagecontrol(page.controls);