javascript form.reset() issue after the postback of an asp.net page
-
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
-
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
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). -
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).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
-
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).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
-
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
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
-
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
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
-
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