473,378 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Set javascript value OnCheckedChanged

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","Javas cript: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.

Nov 19 '05 #1
4 5735
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?

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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","Javas cript: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.

Nov 19 '05 #2
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[i];

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

Nov 19 '05 #3
sorry, there was a Syntax error there.
Should have been:

document.getElementById('cb').checked

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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[i];

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

Nov 19 '05 #4
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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex | last post by:
I need to perform some of Client side manipulation: I have a 4 checkboxes with following them 4 FileUpload HTML controls. What I need to do is to prevent user from uploading file by using 4th...
0
by: Maziar Aflatoun | last post by:
Hi everyone, I am reading and displaying data rows from my database where the first column contains the Status checkbox. I like to enable my users to change the status of individual rows by...
2
by: Maziar Aflatoun | last post by:
Hi everyone, I am reading and displaying data rows from my database where the first column contains the Status checkbox. I like to enable my users to change the status of individual rows by...
5
by: Jason | last post by:
I'm new to WebForms and struggling with very basic stuff at the moment. I am trying to figure out how to detect a radiobutten click state using JavaScript. The following OnCheckedChanged code...
2
by: Asha | last post by:
greetings, i'm loading a datagrid and my gird has checkbox, i want these checkboxex to be check based on the value from the db? can someone please show me the code for it? thanks
2
by: John Walker | last post by:
Hi, On my Webform my ASP:Button does not execute the Javascript code which is 'added' to it on the page load, and I'm trying to figure out why. Please take a look at my code and the error message...
3
by: theycallmetish | last post by:
hey. i'll get right into it. i've got a codebehind page and m presentation page. i've got this code for my checkbox, which in theor is supposed to (when checked) take the first and last name of th...
12
by: tshad | last post by:
I have the following code that attaches a Javascript confirm box to my checkbox. When I select the checkbox, the window comes up fine, but it never executes the XfertoDefault_Click function when I...
3
by: shyamal123 | last post by:
I am write a function in javascript ie function rowcolor(obj) { obj.parentelement.parentelement.style.backgroundcolor='yellow' ; } when i run the appln the error is occure ie the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.