Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

asp.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...