Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 406
Patrice Cote Avatar asked Oct 08 '10 20:10

Patrice Cote


3 Answers

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

like image 65
M4N Avatar answered Nov 11 '22 08:11

M4N


Try changing it to

OnClientClick="ValidateMail(); return false;" 
like image 28
alonso.torres Avatar answered Nov 11 '22 10:11

alonso.torres


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);"
like image 12
Yaron Avatar answered Nov 11 '22 10:11

Yaron