Check form before unload for changes

A

Assimalyst

Hi,

I have a javascript that checks a form for changes, if it has any it
alerts the user before they navigate away from the page.

it can be disabled on a submit button click by adding

onclick="Javascript:bSubmitted=true;"

to it's tag.

However i have a checkbox that has AutoPostBack=true and would like to
disable it for that too, but if i add

onCheckChanged="Javascript:bSubmitted=true;"

I get a compilation error when the page loads

CS1026: ) expected

Can anyone suggest a fix? The javascript is below, just incase it's
useful.

Thanks

function checkFormStatus(oForm){
if(isDirty(oForm))
event.returnValue = "You have entered form Data without submitting this
form.";
}

var bSubmitted=false;
function isDirty(oForm)
{
if(bSubmitted) return false;
var iNumElems = oForm.elements.length;
for (var i=0;i<iNumElems;i++)
{
var oElem = oForm.elements;

if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)
{
if (oElem.value != oElem.defaultValue) return true;
}
else if ("checkbox" == oElem.type || "radio" == oElem.type)
{
if (oElem.checked != oElem.defaultChecked) return true;
}
else if ("SELECT" == oElem.tagName)
{
var oOptions = oElem.options;
var iNumOpts = oOptions.length;
for (var j=0;j<iNumOpts;j++)
{
var oOpt = oOptions[j];
if (oOpt.selected != oOpt.defaultSelected) return true;
}
}
}
return false;
}
 
E

Eliyahu Goldin

onCheckChanged is a server-side event handler. You need this to add a client
side handler:

myCheckBox.Attributes["onclick"] = "Javascript:bSubmitted=true;";

Eliyahu
 
A

Assimalyst

Thanks for the suggestion, but i'm pretty new to all this.

Could you explain in a little more detail how to do this? I can't get
it to work.

Thanks again.
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top