从JavaScript获取C#静态值 [英] Get c# static values from javascript

查看:59
本文介绍了从JavaScript获取C#静态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目大家晚上好,我想从javascript中的c#中获取静态值对我有任何帮助.


在此先感谢

Meganathan M

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

推荐答案

如果将脚本放在.aspx页或.ascx页中,则可以使用<%=%>访问值后面的代码.标签.

范例:

var a =``& lt;%= Namespace.Localization.LocalizationUtility.GetText("lblSomeKey")%& gt;'';
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;'';


首先,您应该理解,您永远不会在C#和JavaScript之间传递任何值,因为它们的性质完全不同,并且在不同方面起作用:服务器上的C#,客户端上的JavaScript.在您感兴趣的情况下,您只需在服务器端生成整个页面,然后以HTTP响应的形式将其发送给客户端.生成的页面中可以包含JavaScript以及其他内容.从JavaScript的角度来看,您只需将JavaScript常量硬编码到代码中,无论它在C#中是什么.

知道了吗?

像这样的东西:
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:
var helloFromAspNet <%= MyCSharpClass.MyStaticValue.ToString(); %>


或者,以确保MyCSharpClass.MyStaticValue的输出是HTML编码的:


or, to ensure the output of MyCSharpClass.MyStaticValue is HTML-encoded:

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


这篇关于从JavaScript获取C#静态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆