Set javascript value OnCheckedChanged

A

Assimalyst

Hi,

I have a AutoPostBack checkbox and need to set a value bSubmitted =
true when the checkbox is checked in order to prevent a warning message
appearing notifying the user they are navigating away from the page.

I have been trying to add the following in the <script> section of the
<HEAD> section:

addNewSurgeonChkBx.Attributes.Add("onclick","Javascript:bSubmitted=true;");

But it does not seem to work, I still get the error message.

I have also tried to put it on the onLoad section of the C# CodeBehind,
here though it surpresses the error message for all controls, not just
the checkbox.

Any ideas of how to fix this?

Thanks.
 
G

Grant Merwitz

How about letting the JavaScript that pops up the notification check if the
checkbox is checked.

So where is says:

Confirm('Are you sure you want to navigate away from this page');

change that too

if(!Document.GetElementById('addNewSurgeonChkBx').Checked)
Confirm('Are you sure you want to navigate away from this page');


Is that what you looking for?
 
A

Assimalyst

Thanks, tried that but no luck. It stops the script from working for
all controls on the page!? Think it errored, so it could have been the
way i added it. Here's what I had:

function checkFormStatus(oForm)
{
if(isDirty(oForm))

if(!Document.GetElementById('addNewSurgeonChkBx').Checked)
event.returnValue = "You have entered form Data without submitting
this form. \nAny changes you have made will not be saved.";
}

Here's the full original code, if that might give anymore ideas:

// Warns user before page unload
function checkFormStatus(oForm)
{
if(isDirty(oForm))
event.returnValue = "You have entered form Data without submitting
this form. \nAny changes you have made will not be saved.";
}

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;
}

It can be disadbled easily with the submit button by adding in it's
tag:

onclick="Javascript:bSubmitted=true;"

However, adding this to a onCheckedChanged for a checkbox results in a
compilation error, saying ") expected".

Any more ideas?

Thanks
 
G

Grant Merwitz

sorry, there was a Syntax error there.
Should have been:

document.getElementById('cb').checked
 
A

Assimalyst

Thank you very much Grant. Thats almost made it work.

It now behaves correctly when the checkbox changes from false to true.
However, the message still appears when it changes from true to false.
I would like to stop this too.

I tried changing the .checked to .checkedchanged but that makes the
message box appear when the checkbox if changing from true to false and
from false to true.

Any ideas?

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top