Return false is not working in JavaScript

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
function OnSave()
{
var ddlStatus=document.getElementById("ddlStatus");
if(ddlStatus.selectedIndex==1)
{
return confirm('Are you sure want to close this ticket number?');
}
}
 
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?');
 
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)
 
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)
 
Back
Top