简体   繁体   中英

How to pass parameters in when call code-behind function in javascript?

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;

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.

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)%>'

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