First use the as operator instead of is and cast :
TextBox tb = ctrl as TextBox; if (tb != null) { tb.Text = String.Empty; }
Secondly, you can use ITextControl instead of TextBox .
And thirdly, try the following extension method:
public static IEnumerable<T> GetChildControls(this Control control) where T : Control { var children = (control.Controls != null) ? control.Controls.OfType<T>() : Enumerable.Empty<T>(); return children.SelectMany(c => GetChildControls(c)).Concat(children); }
Using:
foreach (var c in this.Page.Controls.GetChildControls<TextBox>()) { c.Text = String.Empty; }
source share