Click here to Skip to main content
16,005,038 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Good evening to all in my project i want to get the static values from c# in javascript any one help to me.


thanks in advance
By
Meganathan M
Posted

If you put your script in .aspx page or .ascx page then you can access the code behind values using <%= %> tag.

Example :

var a = '&lt;%=Namespace.Localization.LocalizationUtility.GetText("lblSomeKey")%&gt;';
 
Share this answer
 
First of all, you should understand that you never pass any values between C# and JavaScript, because they have very different nature and also work on different sides: C# on the server, JavaScript on the client. In the case you are interested in, you simply generate a whole page on the server side and sent it to the client in HTTP response. And the generated page can have JavaScript in it, as well as other stuff. From the standpoint of JavaScript, you simply hard-coded a JavaScript constant into the code, no matter what it was in C#.

Getting the idea?

Something like:
JavaScript
var helloFromAspNet <%= MyCSharpClass.MyStaticValue.ToString(); %>

or, to ensure the output of MyCSharpClass.MyStaticValue is HTML-encoded:
JavaScript
var helloFromAspNet =<%: MyCSharpClass.MyStaticValue.ToString(); %>


Please see:
http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx[^].

—SA
 
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