1

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?

1 Answer 1

2

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;" />
Sign up to request clarification or add additional context in comments.

2 Comments

Your a genius ramby, this seems to have set me along the right track. I have been scratching my head on this one for a while. Any chance your could explain why this would work and my code will not? I am really intreagued.
return true/false in the js function is used to exit from the js function with value true/false so as per your code you can interpret onchange="false" which does not mean to stop the postback. but on the same place if you make it onchange="return false" then it will stop the postback. so you can also avoid postback like onchange="return test();"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.