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?
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?
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
var nUserID = "<%= ids.userID %>"<%= %> but wrap the NullRef check in a method :)More appropriate solution would be to to save your ids.userID to data attribute of some related html element or just create invisible div element with all C# data you want to use in your javascript code as data attributes. And then use jQuery to extract that data in scripts.
<div id="settings" style="display: none;" data-user-id="<%= ids.userID %>"></div>
and javascript:
var nUserID = $('#settings').data('user-id');