将 C# 值传递给 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11021066/
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
Pass C# Values To Javascript
提问by Nick LaMarca
On loading the page I want to pass a value to my javascript function from a server side variable.
在加载页面时,我想将一个值从服务器端变量传递给我的 javascript 函数。
I cannot seem to get it to work this is what I have:
我似乎无法让它工作,这就是我所拥有的:
Asp.Net
网络
protected void Page_Load(object sender, EventArgs e)
{
string blah="ER432";
}
Javascript
Javascript
<script type="text/javascript">
var JavascriptBlah = '<%=blah%>';
initObject.backg.product_line = JavascriptBlah;
</script>
Adding this to the page
将此添加到页面
public string blah { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
blah="ER432";
}
I still am getting an error: CS0103: The name 'blah' does not exist in the current context
我仍然收到错误消息:CS0103:当前上下文中不存在名称“blah”
Also I would like to try and accomplish this without using hdden fields
此外,我想尝试在不使用 hdden 字段的情况下完成此操作
采纳答案by Nick Rolando
I believe your C# variable must be a class member in order to this. Try declaring it at the class level instead of a local variable of Page_Load(). It obviously loses scope once page_load is finished.
我相信你的 C# 变量必须是一个类成员才能做到这一点。尝试在类级别而不是Page_Load(). 一旦 page_load 完成,它显然失去了作用域。
public partial class Example : System.Web.UI.Page
{
protected string blah;
protected void Page_Load(object sender, EventArgs e)
{
blah = "ER432";
//....
回答by mlorbetske
In this particular case, blahis local to Page_Loadyou'll have to make it a class level member (probably make it a property) for it to be exposed like that.
在这种特殊情况下,blah是本地的,Page_Load您必须使其成为类级别成员(可能使其成为属性)才能像这样公开。
回答by Matthew
You should declare blahat class level.
您应该blah在类级别声明。
回答by bluevector
Your string blahneeds to be a public property of your Pageclass, not a local variable within Page_Load. You need to learn about scoping.
您string blah需要成为类的公共属性Page,而不是Page_Load. 您需要了解范围界定。
public class MyPage
{
public string blah { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
blah = "ER432";
}
}
回答by OnResolve
What I've done in the past is what webforms does as it's functionality--creating a hidden field to store values needed by the client. If you're using 4.0, you can set the hidden field client id mode to static to help keep things cleaner as well. Basically, add you value to the hidden field and then from javascript you can get the value or modify it too(in case you want to pass it back) since it's just a dom element.
我过去所做的是 webforms 的功能——创建一个隐藏字段来存储客户端所需的值。如果您使用的是 4.0,您可以将隐藏字段客户端 ID 模式设置为静态,以帮助保持更清晰。基本上,将您的值添加到隐藏字段,然后从 javascript 中您可以获取该值或对其进行修改(以防您想将其传回),因为它只是一个 dom 元素。
If you really want to use code--like a variable, it needs to be accessible at the class scope.
如果你真的想使用代码——就像一个变量,它需要在类范围内可以访问。
回答by Dante
You can put a hidden input in your html page:
您可以在 html 页面中放置一个隐藏的输入:
<input type="hidden" runat='server' id="param1" value="" />
Then in your code behind set it to what you want to pass to your .js function:
然后在后面的代码中将其设置为要传递给 .js 函数的内容:
param1.value = "myparamvalue"
Finally your javascript function can access as below:
最后你的javascript函数可以访问如下:
document.getElementById("param1").value

