简体   繁体   English

在嵌入式C#中传递Javascript变量

[英]Passing Javascript Variable in embedded C#

I have a function in C# which can be called like this: 我在C#中有一个函数,可以这样调用:

string result = CachedLanguageItems.GetLabel("RandomLabel").Value

It returns me the cached value of the RandomLabel . 它向我返回RandomLabel的缓存值。
Now I want to use this method in Javascript like this: 现在,我想像这样在Javascript中使用此方法:

var result = <%= CachedLanguageItems.GetLabel("RandomLabel").Value %>

This is also working. 这也起作用。 It returns me the cached value of RandomLabel . 它向我返回RandomLabel的缓存值。

The problem that I am facing now is, that I am not able to pass a javascript variable to the method instead of a hard coded string. 我现在面临的问题是,我无法将javascript变量而不是硬编码字符串传递给该方法。 eg: 例如:

var javascriptVariable= "RandomLabel"
var result = <%= CachedLanguageItems.GetLabel(javascriptVariable).Value %>

Can somebody tell me how to pass the javascript variable correctly to the embedded c# call of the method? 有人可以告诉我如何将javascript变量正确传递给方法的嵌入式c#调用吗?

Do I have to escape the embedded code for this? 为此,我是否必须转义嵌入式代码?
Is this even possible? 这有可能吗?

Thanks for the help. 谢谢您的帮助。

This is not possible. 这是不可能的。 The C# code is evaluated on the server before the HTML is shipped to the browser. 在将HTML传送到浏览器之前,先在服务器上评估C#代码。 The JS code runs on the client (the browser). JS代码在客户端(浏览器)上运行。 What you could do it write a small service which you call via AJAX that returns the value you need. 您可以做些什么,编写一个小型服务,通过AJAX调用该服务,该服务将返回所需的值。

Please read my related answer to this on How to use PHP with Visual Studio 请阅读有关如何在Visual Studio中使用PHP的相关答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM