C#一次性清空TextBox或者将某一类控件置某一状态

来源:互联网 发布:中国修正主义知乎 编辑:程序博客网 时间:2024/06/07 16:09
  1. #region 清除
  2.     protected void btnClear_Click(object sender, EventArgs e)
  3.     {
  4.         foreach (Control ctl in this.Controls)
  5.         {
  6.             this.txtClear(ctl);
  7.         }
  8.     }
  9.     #endregion
  10.     private void txtClear(Control ctls)
  11.     {
  12.         if(ctls.HasControls())
  13.         {
  14.             foreach (Control ctl in ctls.Controls)
  15.             {
  16.                 txtClear(ctl);
  17.             }
  18.         }
  19.         else
  20.         {
  21.             if (ctls.GetType().Name == "TextBox")
  22.             {
  23.                 TextBox tb = new TextBox();
  24.                 tb = (TextBox)this.FindControl(ctls.ID);
  25.                 tb.Text = "";
  26.             }
  27.             else if (ctls.GetType().Name == "DropDownList")
  28.             {
  29.                 DropDownList ddl = new DropDownList();
  30.                 ddl = (DropDownList)this.FindControl(ctls.ID);
  31.                 ddl.SelectedIndex = 1;
  32.             }
  33.         }
  34.     }
原创粉丝点击