Return false is not working in JavaScript

G

Guest

Hi,
I'm having an ASP.net page with a dropdown list box named ddlStatus, a
button and editable gridview.
I wrote a small javascript function "OnSave()" and called it on the event
'onClientClick()' event of the button.
I have a required field validator for the text boxes in the grid view also.
The dropdown list box contains 2 values 'Open' Or 'Close'.
If the user clicks the Button by selecting the 'Close' then i have to
display a confirmation message.
I wrote the following function in javascript
function OnSave()
{
var ddlStatus=document.getElementById("ddlStatus");
if(ddlStatus.selectedIndex==1)
{
if(confirm('Are you sure want to close this ticket number?')==false)
{
return false;
}
}
}

On clicking the Button, i'm getting a confirmation message with OK and
Cancel.After clicking Cancel,return false is not working and the server side
code is being called.

Thanks in advance
Srinivas
 
M

Mark Rae

function OnSave()
{
var ddlStatus=document.getElementById("ddlStatus");
if(ddlStatus.selectedIndex==1)
{
return confirm('Are you sure want to close this ticket number?');
}
}
 
E

Eliyahu Goldin

Two points:

1. To make it work set OnClientClick="return OnSave();"

2. To eliminate some unnecessary lines of code replace
if(confirm('Are you sure want to close this ticket number?')==false)
{
return false;
}
with just
return confirm('Are you sure want to close this ticket number?');
 
B

bruce barker

if you are running any validators, the button calls client script to
postback, so you need to do the following (works with and without a
validator):

<asp:button onclientclick="if (OnSave() == false) return false; />

-- bruce (sqlwork.com)
 
G

Guest

It's working
Thanks a lot...


bruce barker said:
if you are running any validators, the button calls client script to
postback, so you need to do the following (works with and without a
validator):

<asp:button onclientclick="if (OnSave() == false) return false; />

-- bruce (sqlwork.com)
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top