Having issues where in my .aspx page I pass in a boolean variable (C#) to a javascript function that is expecting a boolean type.
BUt the C# variable returns True, and javascript doesn't like the uppercase.
myjavascript( <%= MyBooleanVariableInCSharp %> );
If I convert the c# variable to string, then my javascript variable becomes a string and not a js bool value!
what is the solution to this nightmare? lol
试试这个:
myjavascript( <%= MyBooleanVariableInCSharp.ToString().ToLower() %> );
if you need to do this often, just add this to the top of the javascript (or your js library file, etc.)
var True = true, False = false;
Then you code
myjavascript( <%= MyBooleanVariableInCSharp %> );
Would work just fine.
Another option if for whatever reason you don't want to use the variables is to write your javascript call like this:
myjavascript( '<%= MyBooleanVariableInCSharp %>'=='True' );
你也可以这样做。
myjavascript(<%=myBooleanVariableInCSharp ? "true" : "false" %>);
The other answers are targeting the old version, before Razor
if you are using Razor then this is the solution
myjavascript( @MyBooleanVariableInCSharp.ToString().ToLower() );
C# has a built-in method for it.
At least in .NET v4 and newer.
var myBoolean = <%= myBooleanFromCodeBehind.ToJs() %>;
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.