how to use foreach loop to edit textboxes

c#

Solution

The error is because your controls collection doesn't only contain text boxes. Try this instead.

foreach (Control c in this.Controls)
{
    TextBox t = c as TextBox;

    if (t != null)
    {
        t.text=" ";
    }
}

Problem

``` foreach(textbox t in this.controls) { t.text=" "; } ``` I want to clear all the textboxes in my page at one time. I am getting an error: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

Original source