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 badges1 Answer
Reset to default 9You 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
版权声明:本文标题:c# - How to pass a bool to a JavaScript function from code behind? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741323166a2372324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论