简体   繁体   中英

c# change value of javascript variable from code behind

I have a variable named "tempVariable" in my jquery file. now I need to change its value from code behind in c#. What I have done till now is

in my C# code

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

my jquery code is as

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

The issue is that ChangeValue() function never gets hit.

Am I going wrong somewhere?

I would check in a JS debugger to see if you are getting any errors.

But generally try using 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.

As other people have mentioned, this is nothing to do with jQuery.

may be '...' is required to pass value...

Try this :

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

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