asp.net next traverses the code for all specified controls on the page

  • 2020-05-07 19:30:05
  • OfStack

1. Walk through all TextBox on the page and set the value to 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. Recursive traversal
 
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()) 
{ 
// recursive  ( important )  Otherwise, the program exits  
FindAllTextBoxByPageControl(controlCollection[i].Controls); 
} 
} 
} 

A method is called
 
FindAllTextBoxByPageControl(Page.Controls); 

Related articles: