ASP.NET OnClientClick=”return false;” doesn’t work

I just want to add some client side (JQuery Javascript) validation in a web user control. I put an OnClientClick handler and the function gets called. BUT, even if I return “false”, the OnClick method always get fired. What am I doing wrong ?

I’m with VS 2010, targeting the 4.0 framework with JQuery 1.4.2. and JQuery UI 1.8.4.

Here’s a sample code :

<td style="text-align:right"><asp:Button ID="btnAddSave" OnClientClick="return ValidateMail();" OnClick="btnAddSave_Click" runat="server" Text="Submit" /></td>

Script method :

function ValidateMail() {
alert("Bouton clicked");
return false;

}

If I put a breakpoint in the Page_Load event, I see that I get in and the btnAddSave_Click event is also executed.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

for some reason, although I didn’t have any jquery event handlers attached, it didn’t work.

What actually worked was:

OnClientClick="if (validate_form() == false) return(false);"

Method 2

Do you have a click event handler (registered via jquery) which returns true? In that case, the return value of OnClientClick is ignored.

Have a look at my question (and answer): Why doesn’t returning false from OnClientClick cancel the postback

Method 3

Try changing it to

OnClientClick="ValidateMail(); return false;"

Method 4

Great answers. I do it this way. Same result- the OnClick event is not fired off if false is returned from Java function.

OnClientClick = "if (!ValidateDelete()) return false;"
OnClick="btnDeleteSupplier_Click"

Method 5

oddly enough this worked for me:

OnClientClick="javascript:SubmitTableData(); return CanSubmit();"

set the return value submit with the first function call:

function CanSubmit() {    
    return submit;
}

Hope this helps someone.

Method 6

I’m writing this answer only because I’m using html buttons with ASP.NET WebForms and I couldn’t find solution why should I replace my piece of code with working examples. Here is solution why it is not working. Hope it will help you to understand the issue like checking it helped me. This is my first post, so sorry for style.

<button type="button" id="buttonAddOrEdit" class="btn btn-success"  runat="server" onclick="return myValidate()"    onserverclick="buttonAddOrEdit_ServerClick">Zapisz</button>

And javascript function:

function myValidation() {
        if (validator.form()) {
           //Project logic
            return true;
        }
        else return false;
    };

Using client side click after correct validation wasn’t triggering event on server side that was binded to button. Solution mentioned to change piece of code to:

onclick=”if(!myValidation()) return;”

Works because of way that html rendered on page with onserverclick is created. Onserverclick on html button is being replaced by __doPostBack method from javascript. Fullcode of html button rendered on client side looks this way:

<button onclick="return myValidation(); __doPostBack('ctl00$PortalContent$buttonAddOrEdit','')" id="ctl00_PortalContent_buttonAddOrEdit" type="button" class="btn btn-success">Zapisz</button>  Błąd składni

And after replacing it with if statment.

<button onclick="if(!myValidation()) return; __doPostBack('ctl00$PortalContent$buttonAddOrEdit','')" id="ctl00_PortalContent_buttonAddOrEdit" type="button" class="btn btn-success">Zapisz</button>

Working with return myValidate(); won’t tirgger event because it returns before __doPostBack.

Following code will allow you to add some another code to button if neccessary and won’t cause any issues.

Method 7

Just add javascript:

Example:

javascript:return ValidateMail();

Method 8

Try to set a hidden button with that OnClick attribute like:

<asp:Button ID="Button6" runat="server" onclick="btnAddSave_Click" Text="Button" style="visibility: hidden;" />

remove the OnClick attribute from the button and return false in the OnClientClick attribute like:

<td style="text-align:right"><asp:Button ID="btnAddSave" OnClientClick=" ValidateMail(); return false;"  runat="server" Text="Submit" /></td>

click the hidden button to fire the OnClick method depend on condition in script method:

function ValidateMail() {
 if(true)
 {
    $('#Button6').click();
 }
 else
 {
    //do something
 }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x