I ran into an annoying problem: the following code gives a warning in Visual Studio.
<script type="text/javascript">
var x = <%: ViewData["param"] %>;
</script>
The warning is "Expected expression". Visual Studio gets confused, and all the javascript code after that is giving tons of warnings. Granted, it's all warnings, and it works perfectly fine in runtime - but it is very easy to miss real warnings among dozen of false positives.
开发者_开发技巧It was working the same way in VS2008, and it wasn't fixed in VS2010. Does anybody know if there is a workaround, or a patch?
You need to wrap the server side expression in quotes.
<script type="text/javascript">
var x = "<%: ViewData["param"] %>";
</script>
This is what I got from Microsoft:
Unfortunately, this is due to a design limitation of our editor. We have had multiple users provide the same feedback, but we do not have a good solution at this time, other than to avoid server-side generation of client script. We will consider this issue in our planning for the next Visual Studio release.
You can trick the IDE using quotes or line comments
original code:
<script type="Text/javascript">
<%If Page.IsPostback Then%>
alert("my javascript code");
<%End If%>
</script>
solution:
<script type="Text/javascript">
//<%If Page.IsPostback Then%>
alert("my javascript code");
//<%End If%>
</script>
The approach you're taking is not a particularly sturdy one. Obviously it wouldn't work if you wanted to move your Javascript code to a separate file in order to improve your page load times....
You're better off using hidden form fields to move data from the server to client script. Alternatively, you can build the variable setter JS code programmatically by doing a bunch of string concatenation, then using the ClientScriptManager.RegisterClientScriptBlock()
method to inject it into the output.
I believe that you ask from visual studio to understand too difficult thinks.
How visual studio can know what is inside the string that you pass ?, its parameters, its more code, what it is ???.... How VS can know what it is so can tread them that way ?
So they decide that everything on script tag there must be JavaScript.
My opinion is that if you won to avoid this error, Write render this JavaScript on code behind and not inside aspx page.
string cPlaceMeOnScript = "<script type=\"text/javascript\">var x =" + ViewData["param"] +";</script>"
and use any method to place this string on the start of your script.
I can't believe how many responses say "Put the JavaScript in the code-behind instead". This is a very bad idea, and muddles your code by mixing presentation with business logic. As the original poster stated, this is only a mere warning. While annoying, it's not worth mixing your presenatational behavior in with your server-side code (a.k.a. writing poor code) over.
This is not the standard behavior, and it is just happening randomly, and in such a situation just renaming the file and then building will help, (and it is possible that even without building it will be solved), and you can then revert back.
You can also completely disable warnings on Java Script under Tools -> Options -> Text Editor -> JScript -> Miscellaneous.
Of course in this case you will miss the Java Script warnings, but at least you will be able to concentrate on the server side warnings.
精彩评论