I have searched for a definitive answer to my question and not been able to find one. Lots a site cover the topic but I cannot find the answer I am looking for.
I have an ASPX page that, for example, processes customer queries. In the code behind I have a Customer class. If I want to use, for example, the customer ID and Name in a JavaScript function. What is the best way for me to do it?
I have this code (a website I have been asked to update) but I try to sort out all warnings VS displays and this code displays two.
<script type="text/javascript">
function DoSomeThingFunction(enable)
{
var enableStr = enable ? "on" : "off";
var intClientID = <%= Customer.ClientID %>;
var strClientName = <%= Customer.ClientName %>;
<!--
More functionality
-->
};
</script>
Expected expression on the following two lines..
var intClientID = <%= Customer.ClientID %>;
var strClientName = <%= Customer.ClientName %>;
I can update this code to stop the warnings..
<script type="text/javascript">
function DoSomeThingFunction(enable)
{
var enableStr = enable ? "on" : "off";
var intClientID = parseInt('<%= Customer.ClientID %>');
var strClientName = '<%= Customer.ClientName %>';
<!--
More functionality
-->
};
</script>
My question is: What is the best method of achieving this without getting any kind of warning? Does the best method mean I will get a warning and therefore have to put up with it? I hate to see warnings of any kind and try to fix them all.
Thanks for looking
The first code block is how I have done it in the past. If the value is a string single or double quoted. Numbers are ok to not have quotes.
Another approach is to use hidden fields in the page.
I don’t worry about VS warnings, in fact I rarely look at them. Most of the time the code will still work as expected and especially when it comes to html, css and javascript the warnings are obsolete.