CHALLENGE : Detect changes on form in a generic way

J

JonBosker

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
 
M

Matt Kruse

JonBosker said:
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/validations/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 ;)
 
J

JonBosker

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

for (i=0;i<theform.elements.length;i++) {
var changed=false;
var name=theform.elements.name;
if(!isBlank(name)){
var type=theform.elements.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.name;
if(!isBlank(name)){
var type=theform.elements.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 </BODY> and
<HEAD> tags:
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top