zoukankan      html  css  js  c++  java
  • 遍历aspx页面中所有的指定控件

    //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);

  • 相关阅读:
    HTML和CSS 基本要点必看
    CSS
    六个选择器
    HTML初级课程 (自学可懂)
    this
    1.作用域链
    js if 语句
    js数据类型
    ifelse语句
    三元运算符
  • 原文地址:https://www.cnblogs.com/xiaolei1314/p/3439497.html
Copyright © 2011-2022 走看看