Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to access a string variable in the code behind into javascript
Posted

The best way would be for the code behind to save the string into a Cookie, and have the javascript retrieve it from there.
 
Share this answer
 
Comments
DominicZA 3-Apr-12 9:46am    
I dont totally disagree with you here, but can you take a look at my answer and explain why this would be better?

-Dom
In your code behind:
C#
protected string MyString = "This is my string";


Then, in your HTML page:
JavaScript
function GetMyString() {
     return <%= MyString %>
}


You can obviously put the snippet <%= MyString %> where ever you want to use that string. Hoe this helps!
 
Share this answer
 
Comments
[no name] 3-Apr-12 9:53am    
No reason to use a function

var myString = <%= MyString %>
Or
myString = <%= MyString %>

to place it in the global namespace
DominicZA 3-Apr-12 9:54am    
Yip, thats why I put that comment afterwards saying he could put the snippet anywhere. I just put it in a function to illustrate the point ;)

-Dom
You can pass it as JSON object and retrieve it from javascript.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900