简体   繁体   中英

How to prevent execution of serverside function using javascript?

I am trying to test for a textbox value.

If the value matches my criteria do a javascript function

ELSE

do textchanged event in code behind.

I have a jquery function that is triggered using javascript on change

JAVASCRIPT

function test() {
  if ($('#<%= txt.ClientID%>').val() == '1') {
      return false;
    }
};

ASP.NET

<asp:TextBox ID="txt" runat="Server" AutoPostBack="true" onchange="test(this)" />

VB.NET

Protected Sub txt_TextChanged(sender As Object, e As EventArgs) Handles txt.TextChanged
...
End Sub

While debugging the function clearly returns false however the textchanged event still causes a postback.

THE MARKUP RENDERS AS

onchange="test(this);setTimeout('__doPostBack(\'ctl00$ctl00$ctl00$Master$Content$txt\',\'\')', 0)"

QUESTION

How can the textchanged event be restricted to only execute when certain client side criteria are met?

try to return false inline of textbox as below:-

function test() {
    if ($('#<%= txt.ClientID%>').val() == '1') {
        return false;
    }
    return true;
};


<asp:TextBox ID="txt" runat="Server" AutoPostBack="true" onchange="if(!test(this)) return false;" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM