tags:

views:

65

answers:

2

Suppose I have a function in code behind like:

Public Function Calc(ByVal ID As Integer) As Boolean
  '......
End Function

I can call this function in javascript like

var isSuccess ='<%=Calc()%>'; 

but how to pass the parameter in for this case? Following code not working:

var ID = 1;
var isSuccess ='<%=Calc(ID)%>'; 

Here ID should be got from html element, like var ID= document.getElementById("txtID").value;

+1  A: 

The function is evaluated at the time the page is rendered (on the server), and that var ID = 1 is ignored/not seen...it has nothing to do with your server-side code. The closest you can get would be:

var isSuccess ='<%=Calc(1)%>'; 

If you want to query this in the page in javascript dynamically, as the user interacts with the page...well that's not really how it works, you would need an AJAX callback and a static page method to do this.

Nick Craver
Thanks. my situation for this case is: the app is .net 1.1 app with no AJAX available for it. how to solve this problem?
KentZhou
@KentZhou: AJAX is still possible with a .NET 1.1 backend. It's not that complicated either. However if `Calc()` does not use any server-side resource (such as a database), you might be able to translate the function to JavaScript, and use it like any other function.
Daniel Vassallo
Thank you. Calc() do use database as server-side resource. I will try AJAX.
KentZhou
+1  A: 

The code var ID = 1; will be evaluated inside the browser, but Calc() is a server-side function. The browser knows nothing about Calc() and the server knows nothing about var ID. That is the reason why it will not work. You cannot mix data or logic between the server-side and client-side like that.

As Nick suggested, you may want to use: var isSuccess = '<%=Calc(1)%>'

Daniel Vassallo
Thanks, var ID is a variable, I put it as 1 just for demo, it should be something like var ID= document.getElementById("txtID");
KentZhou
@KentZhou: Does `Calc()` make any use of server-side resources, such as a database? If not, you might be able to write the `Calc()` function in JavaScript, and then simply call it like any other function. If it does use server-side stuff, your only option is to use AJAX.
Daniel Vassallo