How to prevent PostBack on the client side?

I have some client-side JS verification code that needs to be executed for PostBack. If this verification code returns false, the postback is useless. How can this be disabled?

+3
source share
5 answers

Remember that real verification should always be performed on the server. All that you do on the client side is simply optimization to save several trips around the environment.

The easiest way to keep client-side and server-side validation in sync with ASP.Net is to use validation elements. Validation elements will check both on the client side and on the server side, so that when verification fails on the client, it is never sent to the server.

If you want to do something that is not provided for by standard validation elements, you should either use CustomValidator or inherit your own control from BaseValidator.

+4
source

Set OnClientClick = 'YourJSValidationFunction' on your ASP page.

Then return true or false.

False will prevent postback

: http://vijaymodi.wordpress.com/2007/06/08/button-onclick-and-onclientclick-2/

+3

, - :

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return IsValid();" />

IsValid false, postback . , , ,
<form id="form1" runat="server" onsubmit="return IsValid();">

+2

: - onclick?

<input type="button" id="btnID" runat="server" onclick="CheckValid();"/>

function CheckValid()
{
   if(!IsValid) return false;//then no post back occer
}
0

, , CustomValidator. .

0

Source: https://habr.com/ru/post/1749594/


All Articles