ASPX convert string to boolean

266 views Asked by At

I have to support some aspx pages and I'm pretty new to apsx. In the below case allWOs = "false", and ends up being passed to a method expecting a Boolean. How need to convert a string to Boolean? Is the below proposed a viable way to do it?

Code:

var allWOs = "<%= allWorkOrders %>";
BillingWork.MovePeriod(allWOs, MovePeriod_Callback);

Error:

Unable to cast object of type 'AjaxPro.JavaScriptString' to type 'AjaxPro.JavaScriptBoolean'.

Proposed:

 var allWOs = $("#<%= allWorkOrders %>").val() != null ? $("#<%= allWorkOrders %>").prop('checked') : false;
1

There are 1 answers

0
user2316116 On

When using var in

var allWOs = "...";

the compiler determines string as the type. So to pass boolean to the method you would need to convert string to boolean, e.g.

bool allWOs = System.Convert.ToBoolean("<%= allWorkOrders %>");

If allWos is not used anywhere, except that method, and no additional validation of allWorkOrders will be required, then the easiest way will be to test equality with "true", or "false" as it was suggested above.

BillingWork.MovePeriod(allWOs == "true", MovePeriod_Callback);