如何遍历页面上某类控件

一般遍历可以采用两种方面:循环和递归,两者执行效果一样

下面以遍历TextBox为例说明:

一,循环遍历:

 1 protected void Find()
 2ExpandedBlockStart.gifContractedBlock.gif     {
 3        HtmlForm from = (HtmlForm)this.FindControl("form1");
 4        for (int i = 0; i < from.Controls.Count; i++)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif         {
 6             if (from.Controls[i] is TextBox)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif             {
 8                 TextBox tb = from.Controls[i] as TextBox;
 9                 tb.Text = "";
10            }

11        }

12    }

二,递归遍历:

 1protected void Find(Control c)
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3      if (c.Controls != null)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif      {
 5        foreach (Control x in c.Controls)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 7          if (x is System.Web.UI.WebControls.TextBox)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif         {
 9            ((System.Web.UI.WebControls.TextBox)x).Text = "";
10          }

11         Find(x);
12        }

13      }

14    }

转载于:https://www.cnblogs.com/luckeryin/archive/2008/07/23/1249135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值