Narazana Narazana - 1 year ago 69 Question

Access variable from code behind via javascript

I have the following code that I want to return to a variable "t" in javascript:

Code behind:

Public Shared Function GetSomeText() As String
Dim result = "This is from code behind"
Return result
End Function

Caller variable in javascript:

//This is not working like that, I think
var t = GetSomeText();

So, how can I make variable "t" get the "result" from Function GetSomeText from code-behind?

Thank you.

Answer Source

Try this -- assuming that this a public method on the page. This will call the method GetSomeText() on the page class and then do a Response.Write() of the data to the page as it's being rendered. The result should end up between the single quotes in your javascript.

 var t = '<%= GetSomeText() %>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download