从 JavaScript 访问 C# 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9458494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access C# Variable From JavaScript
提问by John Doe
I have a public property in my code behind named Tab which I'm trying to access in the pages aspx file with javascript but I'm not sure how to get the right value.
我的代码中有一个名为 Tab 的公共属性,我试图使用 javascript 在页面 aspx 文件中访问它,但我不确定如何获得正确的值。
This gives me the value I want
这给了我我想要的价值
alert('<% Response.Write(this.Tab); %>');
This does not
这不
var x = <% =this.Tab %>;
alert(x);
Any ideas?
有任何想法吗?
采纳答案by epascarello
If you view the source you are probably seeing
如果您查看源代码,您可能会看到
var x = mystring;
I would guess you want the quotes too
我猜你也想要引号
var x = "<%= this.Tab %>";
Instead of having code inline, why don't you look at RegisterStartUpScript or RegisterClientScriptBlock.
与其让代码内联,不如看看 RegisterStartUpScript 或 RegisterClientScriptBlock。
回答by Tesserex
if this.Tabis a string instead of a number, the JS will break because you didn't put quotes around it in the second example.
如果this.Tab是字符串而不是数字,则 JS 将中断,因为您没有在第二个示例中将引号括起来。
var x = '<%= this.Tab %>';
alert(x);
回答by Pointy
What about
关于什么
var x = "<% =this.Tab %>";
? It depends on what the value is of course, but you have to generate valid JavaScript. Indeed, if it's a string, you'll probably want to do more than just quote it unless you have complete control over its value and you know for sure that the value itself won't contain a quote character.
? 这当然取决于值是什么,但您必须生成有效的 JavaScript。事实上,如果它是一个字符串,您可能想要做的不仅仅是引用它,除非您完全控制它的值并且您确定该值本身不会包含引号字符。
回答by DotNetUser
Two methods to do it, one is
有两种方法可以做到,一种是
var y = '<%=this.Tab %>';
You can also use JavaScriptSerializer in the code behind and have a method to return value of your variable to javaScript code. This helps if you want to return your private data as well and also you can implement more logic to get the values if you want.
您还可以在后面的代码中使用 JavaScriptSerializer 并有一个方法将变量的值返回到 javaScript 代码。如果您还想返回您的私人数据,这会有所帮助,并且您还可以根据需要实现更多逻辑来获取值。
Your code behind-
你背后的代码-
protected string GetTabValue()
{
// you can do more processing here
....
....
// use serializer class which provides serialization and deserialization functionality for AJAX-enabled applications.
JavaScriptSerializer jSerializer=new JavaScriptSerializer();
// serialize your object and return a serialized string
return jSerializer.Serialize(Tab);
}
Your aspx page
你的aspx页面
var x = '<%=GetTabValue()%>';
// called server side method
alert(x);
You can also user javascript eval function to deserialize complex data structures.
您还可以使用 javascript eval 函数来反序列化复杂的数据结构。
回答by Deepakmahajan
It will work
它会工作
var x = '<% =this.Tab %>';
alert(x);
回答by nPcomp
For Razor following works.
对于 Razor 以下作品。
var x = '@yourc#variable';alert(x);
var x = '@yourc#variable';alert(x);

