ASP.NET/JavaScript - Why not "Return Wrong", not preventing postback?

It seems to me that I have done this script many times, and it usually works, so I clearly do not see anything.

Here is my ASP.NET server button:

<asp:Button ID="btnFoo" runat="server" Text="Foo" CssClass="button foo" OnClientClick="foo_Click();" /> 

Which will be displayed on the client as:

 <input type="submit" name="reallylongclientid" value="Foo" onclick="foo_Click();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(reallylongclientidandpostbackoptions, false, false))" id="reallylongclientid" class="button foo"> 

There are no surprises.

Here is a surprise in my JavaScript function:

 function foo_Click() { return false; } 

Okay, so for me it is more, but I cut it to prove the point.

When I click the button, it calls the function on the client side and returns false.

But it still goes back to the server, why?

Basically I want to do this with the click of a button:

  • Client side confirmation.
  • If verification passes, send a message back
  • If not, show some error messages on the form.

Of course, I could change this to a client-side button (input type = "button") and manually start the postback whenever I want, but I don't need to do this. Or should I?

+7
javascript postback
source share
1 answer

write a return , so when you click on the button, it return false , which does not allow you to submit a form

  <asp:Button ID="btnFoo" runat="server" Text="Foo" CssClass="button foo" OnClientClick="return foo_Click();" /> 
+17
source share

All Articles