1. traverse all TextBox in the page and set the value to String. Empty
Copy codeThe Code is as follows:
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. Recursive Traversal
Copy codeThe Code is as follows:
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 ())
{
// Recursion (important). Otherwise, the program will exit.
FindAllTextBoxByPageControl (controlCollection [I]. Controls );
}
}
}
Call Method
Copy codeThe Code is as follows:
FindAllTextBoxByPageControl (Page. Controls );