asp.net下遍历页面中所有的指定控件的代码
程序员文章站
2024-03-08 10:46:28
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);
复制代码 代码如下:
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);
上一篇: 1-6、讯为系统编程adc
下一篇: Java将字节转换为十六进制代码分享