简体   繁体   中英

Passing Javascript Variable in embedded C#

I have a function in C# which can be called like this:

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

It returns me the cached value of the RandomLabel .
Now I want to use this method in Javascript like this:

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

This is also working. It returns me the cached value of 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. 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?

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. The JS code runs on the client (the browser). What you could do it write a small service which you call via AJAX that returns the value you need.

Please read my related answer to this on How to use PHP with Visual Studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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