chris s
chris s

Reputation:

Is there an easy way to clear an ASP.NET form?

is there an easy way to reset ALL text fields in an asp.net form - like the reset button for html controls?

Upvotes: 11

Views: 26848

Answers (7)

Bat_Programmer
Bat_Programmer

Reputation: 6851

<input type="reset" value="Clear" />

Upvotes: 5

ocean4dream
ocean4dream

Reputation: 5182

This works for me:

<asp:Button ID="btnReset" runat="server" Text="Reset" 
OnClientClick="this.form.reset();return false;" />

Upvotes: 7

Dubs
Dubs

Reputation: 420

The easiest way to clear all controls in your form on a submit is:

form1.Controls.Clear()

Upvotes: -1

Stefan
Stefan

Reputation: 1739

This should work:

function resetForm() 
{ 
   var inputs = document.getElementsByTagName('input'); 
   for(var i=0;i<inputs.length;i++) 
   { 
       if(input[i].type == 'text')
          input[i].value = "";
   }
}

Upvotes: 0

aanund
aanund

Reputation: 1493

Depends on your definition of reset. A trivial way to do something like this could be a button with codebehind:

Response.Redirect(Request.Url.PathAndQuery, true);

Or a variation thereof.

Upvotes: 8

tvanfosson
tvanfosson

Reputation: 532455

Using javascript you can do:

document.forms[0].reset();

or

theForm.reset();  // at least with ASP.NET 2.0

As in

<input type='button' id='resetButton' value='Reset' onclick='theForm.reset();return false;' //>

Upvotes: 3

DaveK
DaveK

Reputation: 4607

Some solutions are listed here:

Clear a form in ASP.Net

I was looking for the same solution in ASP.Net to clear my form on the click and I landed on this post. I looked at all the comments and replies. I decided to use the plain old input tag and created a HTML reset button .It worked like a charm, no postbacks, not javascripts. If there is any catch, I couldn't find it...

Upvotes: 1

Related Questions