通过C#值的JavaScript [英] Pass C# Values To Javascript
问题描述
在装载我想一个值从服务器端变量传递给我的javascript功能的页面。
On loading the page I want to pass a value to my javascript function from a server side variable.
我似乎无法得到它的工作,这是我有:
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>
添加此页面
public string blah { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
blah="ER432";
}
我还是我得到一个错误:
CS0103:名称'嗒嗒'不在当前上下文中存在
I still am getting an error: CS0103: The name 'blah' does not exist in the current context
我也想尝试做到这一点,而无需使用hdden领域
Also I would like to try and accomplish this without using hdden fields
推荐答案
我相信你的C#变量必须以这个类成员。尝试在类级别,而不是 Page_Load中的局部变量声明它()
。曾经的Page_Load完成它显然失去范围。
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.
public partial class Example : System.Web.UI.Page
{
protected string blah;
protected void Page_Load(object sender, EventArgs e)
{
blah = "ER432";
//....
这篇关于通过C#值的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!