How to clear all text fields on a page

I have a page with lots of asp.net text fields asp:TextBox. I want to have a clear button that will clear the text of all text fields. Text fields are within their own user control. How can I do that?

+3
source share
6 answers
<input type='Reset' value='clear'/>

Will reset all text fields inside this particular form when clicked.

+4
source

You can use <input type="reset" />for this.

You can assign cssclass to each text field and use jQuery to clear them.

+3
source

jQuery - :

$("#theButton").click(function() {
  $("[type=text]").val("");
});
+3
protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearControls();

    }
    private void ClearControls()
    {
        foreach (Control c in Page.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }
    }
+2

textBox1.Clear();

label1.Text = "";

.

0

Using this method, we can easily clear the text stored in the text box

protected void Reset_Click(object sender, EventArgs e)
{
    ClearInputs(Page.Controls);
}
void ClearInputs(ControlCollection ctrls)
{
    foreach (Control ctrl in ctrls)
    {
        if (ctrl is TextBox)
            ((TextBox)ctrl).Text = string.Empty;
        ClearInputs(ctrl.Controls);
    }
}
0
source

All Articles