Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. javascript form.reset() issue after the postback of an asp.net page

javascript form.reset() issue after the postback of an asp.net page

Scheduled Pinned Locked Moved Web Development
asp-nettutorialcsharpjavascriptsysadmin
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Ken A
    wrote on last edited by
    #1

    hi. i have a simple page with some textboxes, a submit button and a client-side reset button. 1. for this first scenario, i fill the textboxes and the reset button works fine in case i click on it 2. for the second scenario, after i filled the form, i submit it and after the postback, since it's an aspnet page i have the textboxes filled ... in case i click the reset button the textboxes will not be cleared, but it will be reset to the previous values, because now the reset will 'think' that the original values are not the empty ones, but the ones i have filled the form note: i also set the enableviewstate=false to the textboxes note: if i do a server.transfer then i have the textboxes cleared, but this is no option, since after the submit i show a message in the page, plus i don't want to postback a page just to clear the form elements note: i reset the form elements with the following code: 1 2 function ResetForm() 3 { 4 var formId = <%= "'" + Form.ClientID + "'" %>; 5 document.forms[formId].reset(); 6 } 7 someone may say to use a javascript code to look for the form elements i want to reset and set its values to empty, but again why? since i have a built-in reset method so, as far as my understanding goes, form.reset, reset form fields to its original values, not necessarily empty values ... after the postback, the original form values will be the ones i had filled in, like in the second scenario example. any tips on how to properly reset this form? »»» KenA

    B 1 Reply Last reply
    0
    • K Ken A

      hi. i have a simple page with some textboxes, a submit button and a client-side reset button. 1. for this first scenario, i fill the textboxes and the reset button works fine in case i click on it 2. for the second scenario, after i filled the form, i submit it and after the postback, since it's an aspnet page i have the textboxes filled ... in case i click the reset button the textboxes will not be cleared, but it will be reset to the previous values, because now the reset will 'think' that the original values are not the empty ones, but the ones i have filled the form note: i also set the enableviewstate=false to the textboxes note: if i do a server.transfer then i have the textboxes cleared, but this is no option, since after the submit i show a message in the page, plus i don't want to postback a page just to clear the form elements note: i reset the form elements with the following code: 1 2 function ResetForm() 3 { 4 var formId = <%= "'" + Form.ClientID + "'" %>; 5 document.forms[formId].reset(); 6 } 7 someone may say to use a javascript code to look for the form elements i want to reset and set its values to empty, but again why? since i have a built-in reset method so, as far as my understanding goes, form.reset, reset form fields to its original values, not necessarily empty values ... after the postback, the original form values will be the ones i had filled in, like in the second scenario example. any tips on how to properly reset this form? »»» KenA

      B Offline
      B Offline
      badgrs
      wrote on last edited by
      #2

      It resets your values after the postback since the html returned has value="your value" for each field, so the reset uses these values (which are empty on the first request)...but anyway you already got that far. Why are you using javascript for the reset in the first place? Why not use ? Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option. Heres a simple function that should work: function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } } I haven't tested the function and it will only reset input elements (ie not select).

      K V 2 Replies Last reply
      0
      • B badgrs

        It resets your values after the postback since the html returned has value="your value" for each field, so the reset uses these values (which are empty on the first request)...but anyway you already got that far. Why are you using javascript for the reset in the first place? Why not use ? Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option. Heres a simple function that should work: function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } } I haven't tested the function and it will only reset input elements (ie not select).

        K Offline
        K Offline
        Ken A
        wrote on last edited by
        #3

        badgrs wrote:

        Why are you using javascript for the reset in the first place? Why not use ?

        hi ... the reason i'm not using the type="reset" is because i also call another js Page_ClientValidate(); in order to clear any eventual error msg previously displayed in case the user has done some bad format input, other than this i would use your idea.

        badgrs wrote:

        Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option.

        ow, that's what i was afraid of ... but if js is my only option, this leads to the following conclusion: for asp.net pages it's useless to call form.reset or the input type="reset" tag since it will only work before postbacks, plus i will also have to manually track all forms elements i'm eventually using in the webpage, not just input fields, but select fields, option, etc as well what is important to notice is that the built-in form[].reset() goal is really to 'reset' form values to its original values and NOT to clear all form fields, unless the initial values are empty ones!!! but then a doubt raises: isn't asp.net breaking the rules by maintaining form field values after a postback? one of the great things when asp.net brought was keeping form values after a postback, but now i can't use one of the most basic things in html???

        »»» KenA

        V 1 Reply Last reply
        0
        • B badgrs

          It resets your values after the postback since the html returned has value="your value" for each field, so the reset uses these values (which are empty on the first request)...but anyway you already got that far. Why are you using javascript for the reset in the first place? Why not use ? Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option. Heres a simple function that should work: function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } } I haven't tested the function and it will only reset input elements (ie not select).

          V Offline
          V Offline
          Vasudevan Deepak Kumar
          wrote on last edited by
          #4

          badgrs wrote:

          Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option.

          True.

          badgrs wrote:

          function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } }

          Additionally, you may need to accomodate TEXTAREA tags. For Select tags, you can set selectedIndex to -1.

          Vasudevan Deepak Kumar Personal Homepage Tech Gossips

          K 2 Replies Last reply
          0
          • K Ken A

            badgrs wrote:

            Why are you using javascript for the reset in the first place? Why not use ?

            hi ... the reason i'm not using the type="reset" is because i also call another js Page_ClientValidate(); in order to clear any eventual error msg previously displayed in case the user has done some bad format input, other than this i would use your idea.

            badgrs wrote:

            Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option.

            ow, that's what i was afraid of ... but if js is my only option, this leads to the following conclusion: for asp.net pages it's useless to call form.reset or the input type="reset" tag since it will only work before postbacks, plus i will also have to manually track all forms elements i'm eventually using in the webpage, not just input fields, but select fields, option, etc as well what is important to notice is that the built-in form[].reset() goal is really to 'reset' form values to its original values and NOT to clear all form fields, unless the initial values are empty ones!!! but then a doubt raises: isn't asp.net breaking the rules by maintaining form field values after a postback? one of the great things when asp.net brought was keeping form values after a postback, but now i can't use one of the most basic things in html???

            »»» KenA

            V Offline
            V Offline
            Vasudevan Deepak Kumar
            wrote on last edited by
            #5

            Ken.A wrote:

            pages it's useless to call form.reset or the input type="reset" tag since it will only work before postbacks, plus i will also have to manually track all forms elements i'm eventually using in the webpage, not just input fields, but select fields, option, etc as well

            Check out the related thread in the message whose permalink is here: http://www.codeproject.com/script/comments/forums.asp?msg=2099807&forumid=1640#xx2099807xx[^]

            Ken.A wrote:

            isn't asp.net breaking the rules by maintaining form field values after a postback?

            ViewState has the advantage of resurrecting the form values after the roundtrip. Perhaps in your perspective, it is a double-edged sword. :)

            Vasudevan Deepak Kumar Personal Homepage Tech Gossips

            1 Reply Last reply
            0
            • V Vasudevan Deepak Kumar

              badgrs wrote:

              Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option.

              True.

              badgrs wrote:

              function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } }

              Additionally, you may need to accomodate TEXTAREA tags. For Select tags, you can set selectedIndex to -1.

              Vasudevan Deepak Kumar Personal Homepage Tech Gossips

              K Offline
              K Offline
              Ken A
              wrote on last edited by
              #6

              ok ... but your ResetForm() method is too generic ... it will reset all inputs in my webform, eg: type="submit", type"option", etc ... i just want to reset type="text" ... looping through all the input attributes until i find one with type="text" to clear its value ... hum, is there a better way?

              »»» KenA

              1 Reply Last reply
              0
              • V Vasudevan Deepak Kumar

                badgrs wrote:

                Since the values are 'hard-coded' (in the html) upon a postback, javascript is your only option.

                True.

                badgrs wrote:

                function ResetForm() { var inputs = document.getElementsByTagName("input"); for(var i = 0; i < inputs.length; i++) { inputs[i].value = ""; } }

                Additionally, you may need to accomodate TEXTAREA tags. For Select tags, you can set selectedIndex to -1.

                Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                K Offline
                K Offline
                Ken A
                wrote on last edited by
                #7

                a better ResetForm method could be something like: function ResetFormByTagName( tagName, hasValidators ) { var arrTags = document.getElementsByTagName( tagName ); for( var i=0; i »»» KenA

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups