简体   繁体   English

c#从后面的代码更改javascript变量的值

[英]c# change value of javascript variable from code behind

I have a variable named "tempVariable" in my jquery file. 我的jquery文件中有一个名为“tempVariable”的变量。 now I need to change its value from code behind in c#. 现在我需要从c#中的代码中改变它的值。 What I have done till now is 我到现在所做的是

in my C# code 在我的C#代码中

public void changeValueInJquery()
{
    bool newVal = false;
    Page.ClientScript.RegisterClientScriptBlock(
        GetType(), 
        "key", "ChangeValue(" + newVal + ");", true);
 }

my jquery code is as 我的jquery代码是

function ChangeValue(value1) {
    alert(value1);
    tempVariable = value1;
}

The issue is that ChangeValue() function never gets hit. 问题是ChangeValue()函数永远不会被击中。

Am I going wrong somewhere? 我在某个地方出错了吗?

I would check in a JS debugger to see if you are getting any errors. 我会检查一个JS调试器,看看你是否收到任何错误。

But generally try using Page.ClientScript.RegisterStartupScript(); 但通常尝试使用Page.ClientScript.RegisterStartupScript(); instead if you are calling functions already present in the page. 相反,如果您正在调用页面中已存在的函数。 This will ensure that the script block is rendered at the bottom of the page and not for example before the ChangeValue function. 这将确保脚本块在页面底部呈现,而不是在ChangeValue函数之前呈现。

As other people have mentioned, this is nothing to do with jQuery. 正如其他人提到的,这与jQuery无关。

may be '...' is required to pass value... 可能是'...'需要传递价值......

Try this : 尝试这个 :

Page.RegisterStartupScript("changevalue", "<script>ChangeValue('" + newVal + "');</script>");

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

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