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 h开发者_运维知识库ow 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)%>'
精彩评论