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

前端 未结 5 1482
名媛妹妹
名媛妹妹 2021-02-02 05:28

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 j

相关标签:
5条回答
  • 2021-02-02 05:33

    You could also do this.

    myjavascript(<%=myBooleanVariableInCSharp ? "true" : "false" %>);
    
    0 讨论(0)
  • 2021-02-02 05:39

    Try this:

    myjavascript( <%= MyBooleanVariableInCSharp.ToString().ToLower() %> );
    
    0 讨论(0)
  • 2021-02-02 05:39
    function toBool(s){ 
       return s==="True";
    }
    
    var b = toBool("@csharpvariable.ToBoolean()");
    
    0 讨论(0)
  • 2021-02-02 05:53

    The other answers are targeting the old version, before Razor
    if you are using Razor then this is the solution

    myjavascript( @MyBooleanVariableInCSharp.ToString().ToLower() );
    
    0 讨论(0)
  • 2021-02-02 05:59

    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' );
    
    0 讨论(0)
提交回复
热议问题