How can I use inline ASP.NET tags inside JavaScript?
How can I use ASP.NET inline tags from within a block of Java开发者_JAVA技巧Script? For example:
<script type="text/javascript">
// Do some AJAX here, then redirect to a new page on the next line.
window.location = "/Movie/" + <%= html.encode(MovieName) %>;
</script>
Just like you have on the ASP.Net part, but you want it inside the quotes, like this:
window.location = "/Movie/<%= html.encode(MovieName) %>";
Since it echos out to the page, it will render like this:
window.location = "/Movie/MyMovie";
Outside the quotes it would look like this:
window.location = "/Movie/" + MyMovie;
//thinks MyMovie is a variable, not true!
Where is your JavaScript, inline in the aspx template, or in a separate file?
If it's in a separate file, then by default it will not work as expected as the file will not be subject to the ASP.NET processing pipeline.
If it's inline, then how you have it will suffice, although you need to quote the server tags too
<script type="text/javascript">
// Do some AJAX here, then redirect to a new page on the next line.
window.location = "/Movie/<%= html.encode(MovieName) %>";
</script>
精彩评论