遍历页面上所有TextBox,并赋值为String.Empty

//不含母板页
foreach (System.Web.UI.Control txtobj in this.Page.Controls)
 
{
    if (txtobj.GetType().Name .Equals("TextBox"))
    {
       // ((TextBox)txtobj).Text = String.Empty;//这是第一种方法赋值,第二种在下面
        TextBox tb = new TextBox();
         tb = (TextBox)this.FindControl(txtobj.ID);
 
         tb.Text = String.Empty;
    }
}
 

包含母板页


//套用母版页的页面遍历TextBox控件的方法,其他控件类似
 
foreach (Control cp in Page.Controls)   
{   
       foreach (System.Web.UI.Control ct in cp.Controls)   
       {  
            if (ct is HtmlForm)   
            {   
                  foreach (Control con in ct.Controls)  
                  {   
                      foreach (Control c in con.Controls)  
                      {  
                          if (c is TextBox)  
                          {  
                              (c as TextBox).Text = String.Empty;  
                          }  
                      }   
                  }   
             }   
       }   
}

 

你可能感兴趣的:(Net)