21

Is there an easy way to reset all the fields in a form?

I have around 100 controls in my asp.net form and there are submit and reset buttons.

How do I make all values in the fields null when user hits reset button?

I have a lot of dropdown boxes, textboxes, checkboxes.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
acadia
  • 2,301
  • 10
  • 40
  • 57
  • check here http://stackoverflow.com/questions/680241/blank-out-a-form-with-jquery – Claudio Redi Sep 25 '10 at 14:18
  • 2
    @Claudio, the OP is asking how to reset all fields of a form in an ASP.NET application. Nothing is mentioned about jQuery and voting to close this answer as an exact duplicate seems wrong to me. – Darin Dimitrov Sep 25 '10 at 14:32
  • @Darin Dimitrov: maybe you're right. Assuming that everyone is using/ is willing to use jquery (even nowadays) could be too pretentious. Sorry but I can't revert my vote so far I know. BTW: I still thinking that input type reset may led to a confusion. – Claudio Redi Sep 25 '10 at 14:54

7 Answers7

19

Add this to the server-side handler of the cancel button:

Response.Redirect("~/mypage.aspx", false);
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • This would lose all viewstate data, which might not be desirable. – Tomasi Sep 26 '10 at 04:09
  • 6
    If we're clearing out all the fields, we obviously don't need to retain the ViewState in the first place... we're simply re-loading the page afresh which is the simplest way of doing this. He specified that all fields were being cleared :-) – IrishChieftain Sep 26 '10 at 04:45
  • 1
    May I ask why you call redirect with second parameter `true`? Wouldn't you rather use `false` to prevent an `AbortThreadException`? ([Relevant link](https://blogs.msdn.microsoft.com/tmarq/2009/06/25/correct-use-of-system-web-httpresponse-redirect/).) – Protector one Feb 02 '18 at 10:55
16

Loop through all of the controls on the page, and if the control is type TextBox, set the Text property to String.Empty

protected void ClearTextBoxes(Control p1)
{
    foreach (Control ctrl in p1.Controls)
    {
        if(ctrl is TextBox)
        {
             TextBox t = ctrl as TextBox;

             if(t != null)
             {
                  t.Text = String.Empty;
             }
        }
        else
       {
           if (ctrl.Controls.Count > 0)
           {
               ClearTextBoxes(ctrl);
           }
        }
    }
}

Then to call it in your click event like this:

 ClearTextBoxes(Page);
Community
  • 1
  • 1
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
10

Try adding an:

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

to your form.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 4
    So far I know that would initialize forms field to the initial state. If the initial state for a texbox was "HELLO" It will reset to that value. I don't think it's what the user needs. – Claudio Redi Sep 25 '10 at 14:16
10

You can make use of OnClientClick event. This will reset all all the control present on the form. OnClientClick="this.form.reset();return false;"

See the Code :

<asp:Button ID="Reset_Button" runat="server" Text="Reset" 
    Width="81px" OnClientClick="this.form.reset();return false;" />
LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
NightKnight
  • 291
  • 1
  • 5
  • 14
  • it's working before submitting the form only. when i submit the form and then if i click on reset, the form is not getting cleared. – Ranadheer Reddy Jun 03 '13 at 13:44
  • OnClientClick is always called before server side code so what's that point to clear the form before submitting /????? – dnxit Mar 03 '14 at 15:51
  • This worked for me, but how do I make it clear all forms on 1 page, I have a page that has 4 forms for some odd reason, but I want to clear all on a single click – Anton Mar 16 '16 at 07:49
5

The best option from my side would be

Response.Redirect(Request.RawUrl);

Just add this code on the cancel or reset button of your asp.net control.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Sumit Roy
  • 413
  • 2
  • 9
  • 23
1

If you are using asp.net formview object, just use myFormView.DataBind(); in your reset button click event.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
0

Or if you are using Angular.

Add this to your button on the cshtml page:

<input type="button" value="Cancel" id="cancel" ng-click="cancel();" />

And this to your .js file:

$scope.cancel = function () {
    if (!$('form').dirtyForms('isDirty')) {
        $('form').dirtyForms('setClean');
    }
    else {
        $('form').dirtyForms('isDirty', true);
    }
     var that = this;
    var method = that.getUrl('CONTROLLER', 'ACTION', 'id', 'querystring');
    window.location = method;
jay.tech66
  • 2,327
  • 1
  • 13
  • 6