Trying to pass in a boolean C# variable to a javascript variable and set it to true

Try this:

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' );

You could also do this.

myjavascript(<%=myBooleanVariableInCSharp ? "true" : "false" %>);

Tags:

Javascript

C#