admin管理员组

文章数量:1287776

I have a JavaScript function as follows:

function A(bNeed)
{
    if (bNeed){
        ...
    }
    else{
        ...
    }
}

In my code behind, in Page_Load, I have

bool bNeed = File.Exists(...);
btn.Attributes.Add("onclick", string.Format("return A('{0}');", bNeed));

But it doesn't seem to work correctly. Can anyone tell me what is wrong?

I have a JavaScript function as follows:

function A(bNeed)
{
    if (bNeed){
        ...
    }
    else{
        ...
    }
}

In my code behind, in Page_Load, I have

bool bNeed = File.Exists(...);
btn.Attributes.Add("onclick", string.Format("return A('{0}');", bNeed));

But it doesn't seem to work correctly. Can anyone tell me what is wrong?

Share Improve this question edited Nov 16, 2013 at 16:29 Michael Liu 55.5k14 gold badges124 silver badges156 bronze badges asked Nov 8, 2013 at 16:18 GLPGLP 3,68520 gold badges65 silver badges96 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You are passing capitalized 'True' and 'False' as quoted strings, but the JavaScript Boolean literals are lowercase true and false without quotes. Change it to:

btn.Attributes.Add("onclick", string.Format("return A({0});", bNeed ? "true" : "false");

(If you prefer, you could write bNeed.ToString().ToLowerInvariant() instead of bNeed ? "true" : "false" because Boolean.ToString() returns "True" and "False".)

本文标签: cHow to pass a bool to a JavaScript function from code behindStack Overflow