遍历页面上所有的TextBox控件并给它赋值

  1. /// <summary>
  2.         ///  遍历页面控件 当控件名为 classid 则显示该控件
  3.         /// </summary>
  4.         /// <param name="ctl">页面</param>
  5.         /// <param name="classid">控件名</param>
  6.         private void Set_Controls(Control ctl, string classid,bool visb)
  7.         {
  8.             if (ctl is DataList)
  9.             {
  10.                 foreach (Control c in ctl.Controls)
  11.                 {
  12.                     foreach (Control cc in c.Controls)
  13.                     {
  14.                         if (cc.ID == classid)
  15.                         {
  16.                             cc.Visible = visb;
  17.                         }
  18.                         else
  19.                         {
  20.                             // cc.Visible = false;
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.             else
  26.             {
  27.                 //当控件没有子控件时  
  28.                 if (!ctl.HasControls())
  29.                 {// 控件在 页面根节点下  不在其他容器内
  30.                     if (ctl.ID == classid)
  31.                     {
  32.                         ctl.Visible = visb;
  33.                     }
  34.                     else
  35.                     {
  36.                        // ctl.Visible = false;
  37.                     }
  38.                 }
  39.                 else   //当控件有子控件时    
  40.                 {
  41.                     int i = 0;
  42.                     while (i < ctl.Controls.Count)
  43.                     {
  44.                         Set_Controls(ctl.Controls[i], classid,visb);
  45.                         i++;
  46.                     }
  47.                 }
  48.             }
  49.         }
网上能查到的一些解法的问题
第一种,遍历this.Controls
代码如下:
foreach  (Control ctl  in   this .Controls)
if (typeof(ctl)==typeof(TextBox)).

这样并不能遍历整个页面中的TextBox
  1. this.Controls只是包含了Page根一级的control,这样次级的control就都没有遍历
  2. TextBox一般会放在form里面,遍历this.Controls只会访问form control,而不会访问form的子contorl,其中的TextBox
第二种,遍历Controls[1]
代码如下:
for ( int    i = 0 ;i < inPage.Controls[ 1 ].Controls.Count;i ++ )  
  
{  
  
if(inPage.Controls[1].Controls[i].GetType().ToString()=="System.Web.UI.WebControls.TextBox")  

同样的情况:
  1. 这种代码没有通用性,你怎么就知道Control[1]正是你要遍历的collection
  2. 次级的control都没有遍历

我认为正确的做法:使用递归对页面control树进行完全遍历,并对每一个control进行处理。递归算法如下:

  1. 传入page的this.Colletions
  2. 对每一个contorl,如果contorl没有包含子control,进行处理。
  3. 如果包含,递归调用这个函数处理子control
代码:
     private   void  InitialControl(ControlCollection objControlCollection)
    
{
        
foreach (System.Web.UI.Control objControl in objControlCollection)
        
{
            
if (objControl.HasControls())
            
{
                InitialControl(objControl.Controls);
            }

            
else
            
{
                
if (objControl is System.Web.UI.WebControls.TextBox)
                
{
                    ((TextBox)objControl).Text 
= String.Empty;
                }

            }

        }

    }

 

附案例:

 

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class alltextbox : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         Label1.Text = TextBox1.GetType().ToString(); 
  16.             
  17.        
  18.     }
  19.     
  20.     /// <summary>
  21.     /// 
  22.     /// </summary>
  23.     /// <param name="sender"></param>
  24.     /// <param name="e"></param>
  25.     protected void Button1_Click(object sender, EventArgs e)
  26.     {
  27.         
  28.         Set_Controls(this.Page);
  29.         
  30.     }
  31.     private void Set_Controls(Control ctl)
  32.     {
  33.         //当控件没有子控件时   
  34.         if (!ctl.HasControls())
  35.         {
  36.             switch (ctl.GetType().ToString())
  37.             {
  38.                 case "System.Web.UI.WebControls.Label":
  39.                     break;
  40.                 case "System.Web.UI.WebControls.TextBox":
  41.                     ((TextBox)ctl).Text="xzz";
  42.                     break;
  43.                 case "System.Web.UI.WebControls.DropDownList":
  44.                     ((DropDownList)ctl).SelectedIndex = -1;
  45.                     break;
  46.             }
  47.         }
  48.         else   //当控件有子控件时   
  49.         {
  50.             int i = 0;
  51.             while (i < ctl.Controls.Count)
  52.             {
  53.                 Set_Controls(ctl.Controls[i]);
  54.                 i++;
  55.             }
  56.         }
  57.     }  
  58. }

 

附例: 查找datalist中 ID为指定的控件

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值