开发者

How can I use inline ASP.NET tags inside JavaScript?

开发者 https://www.devze.com 2022-12-24 18:26 出处:网络
How can I use ASP.NET inline tags from within a block of Java开发者_JAVA技巧Script?For example: <script type=\"text/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>
0

精彩评论

暂无评论...
验证码 换一张
取 消