Search code examples
javascriptc#asp.netstring-conversion

Assign C# string to Javascript variable


 var nUserID = '<% if (ids != null) {ids.userID.ToString();}%>'

Note: ids is not null nor ids.userID is null but em getting nUserID = ""
Is there any way to convert C# string to Javascript string?


Solution

  • For write it direct on page you need to use the Response.Write as

     var nUserID = <% if (ids != null) {Response.Write(ids.userID.ToString());}%>
    

    beside that, if you need to add at the end ; or place it inside quotas is up to you.

    The <% %> did not write on page, just run the code.
    The <%= %> write on the page as the Response.Write