Coder 2
Coder 2

Reputation: 4881

How to clear all textboxes on a page

I have a page with a whole lot of asp.net textboxes asp:TextBox. I want to have a clear button which will clear the text from all the textBoxes. The textBoxes are all within their own usercontrol. How could this be done?

Upvotes: 3

Views: 6112

Answers (6)

KKDev
KKDev

Reputation: 160

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

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);
    }
}

Upvotes: 0

oldrock
oldrock

Reputation: 871

<input type='Reset' value='clear'/>

Will reset all the text fields inside that particular form when clicked.

Upvotes: 4

shaki
shaki

Reputation: 71

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;
                }
            }
        }
    }

Upvotes: 2

Oscar Arango
Oscar Arango

Reputation: 113

on the clear button event use this

textBox1.Clear();

and for a label you could use this

label1.Text = "";

it's that simple.

Upvotes: 0

sajoshi
sajoshi

Reputation: 2763

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

Altenately you can assign a cssclass to each textbox and use jQuery to clear them.

Upvotes: 3

xanadont
xanadont

Reputation: 7604

jQuery is your friend:

$("#theButton").click(function() {
  $("[type=text]").val("");
});

Upvotes: 3

Related Questions