By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

CHALLENGE : Detect changes on form in a generic way

P: n/a
I have a form developed in ASP.net and as such all of my controls are
server controls and do not have access to onchange events.

What I need is a javascript program to detect changes.

Here is my idea... when the page loads the program cycles through all
of the objects in the form and saves their values into a variable.
Then, when the user attempts to navigate away the program cycles
through all of the objects in the form and saves the values into
another variable. It checks the first variable agains the second and
if different asks the user if they want to discard their changes.

I do not have the javascript skills to do this but maybe someone else
does? Or someone can tell me this is impossible? By the way I am
willing to pay up to $100 US for a reasonable solution.

Jon Bosker
www.dbgurus.com.au

Feb 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
JonBosker wrote:
I have a form developed in ASP.net and as such all of my controls are
server controls and do not have access to onchange events.
What I need is a javascript program to detect changes.
See isFormModified at
http://www.mattkruse.com/javascript/...ns/source.html
Here is my idea... when the page loads the program cycles through all
of the objects in the form and saves their values into a variable.
No need, since the default value is a property of each element and can be
compared to the current value.
By the way I am
willing to pay up to $100 US for a reasonable solution.
I accept Paypal ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 7 '07 #2

P: n/a
Thanks to Matt Kruse (http://www.JavascriptToolbox.comhttp://
www.AjaxToolbox.com) for providing the majority of the solution.

Ideally I would have liked to use the onunload event, or even the
onbeforeunload event but it seems from my research that is not
possible to get either of these events to cancel. Strange but
apparently true.

Matt very kindly provided an unpdate to his code to handle the command
buttons I had on my form
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Modify this part of the function:
for (i=0;i<theform.elements.length;i++) {
var changed=false;
var name=theform.elements[i].name;
if(!isBlank(name)){
var type=theform.elements[i].type;
if(!ignoreFields[name]){
if(type=="hidden"&&hiddenFields[name])
{changed=isChanged(theform[name]);}
else if(type=="hidden"){changed=false;}
else {changed=isChanged(theform[name]);}
}
}
if(changed){return true;}
}
return false;
}

to be this:

for (i=0;i<theform.elements.length;i++) {
var changed=false;
var name=theform.elements[i].name;
if(!isBlank(name)){
var type=theform.elements[i].type;
if(type!="submit" && type!="reset" && !ignoreFields[name]){ // THIS
LINE IS
CHANGED
if(type=="hidden"&&hiddenFields[name])
{changed=isChanged(theform[name]);}
else if(type=="hidden"){changed=false;}
else {changed=isChanged(theform[name]);}
}
}
if(changed){return true;}
}
return false;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

and then for the really simple ones amongst us who have little idea
about javascript etc this is what I did:

1. Modified the file validations.js in the way described above.

2. I then added my own simple function to this file:

function confirmExit()
{
if (isFormModified(document.form2, null, null))
{
return (confirm('Are you sure you wish to lose your unsaved
changes?'))
}
return true;
}

3. I added this line into my ASP.net page between the </BODYand
<HEADtags:
<script src="validations.js"></script>

4. On each of my controls that could potentially cause data loss I
added this command (this is in ASP.net - in javascript I guess it
would be onclick)
OnClientClick="return confirm('Are you sure you want to delete
this record?');"

Thats it. It works. Hope that is helpful for someone else.

Jon Bosker
www.dbgurus.com.au

Feb 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.