DreamTeK
DreamTeK

Reputation: 34177

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?

Upvotes: 1

Views: 1709

Answers (1)

ramby
ramby

Reputation: 96

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;" />

Upvotes: 2

Related Questions