0

i'm new to asp.net and busy with a tutorial website.

I'm trying to call a server side function using an if statement, the problem is that the if statement is completely ignored i.e the function is called regardless.

This works when using a normal js alert

code below:

$(function () {
    $("#idnum").keyup(function () {
        if (this.value.length == 13) { 
            <%= TextBox1_TextChanged(this, idnum.Text) %> ; 
            //call code behind function to get contact info with ID
        }
    });
});
Anton
  • 32,245
  • 5
  • 44
  • 54
Beamo
  • 47
  • 1
  • 6
  • You can't call server methods like that. Your method is called when the page is generated not by the JavaScript code. – Stilgar Feb 07 '14 at 13:40
  • You just can't do this. The fact it works with an alert is because you're rendering text from server, then executiong client code (javascript). – Laurent S. Feb 07 '14 at 13:41

1 Answers1

2

You can't invoke server side methods on client with your code. Try to use PageMethods in order to call server side method from client.

hkutluay
  • 6,794
  • 2
  • 33
  • 53
  • i have used the PageMethods as below but this is not calling the function – Beamo Feb 07 '14 at 14:06
  • $(function () { $("#idnum").keyup(function () { if (this.value.length == 13) { PageMethods.getID(this.value); //call code behind function to get contact info with ID } }); }); – Beamo Feb 07 '14 at 14:06
  • Before running the code don't forget to include the Script Manager in your aspx page and also enable the page methods. – hkutluay Feb 07 '14 at 14:07
  • i have also tried to use scriptmethod as suggested here [link](http://stackoverflow.com/questions/10658061/calling-asp-net-page-method-from-javascript-not-working) – Beamo Feb 07 '14 at 14:12