简体   繁体   English

如何在JavaScript中使用内联ASP.NET标记?

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

How can I use ASP.NET inline tags from within a block of JavaScript? 如何在JavaScript块中使用ASP.NET内联标记? 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: 就像你在ASP.Net部分,但你想在引号内,如下所示:

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? 您的JavaScript在哪里,在aspx模板中内联,还是在单独的文件中?

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. 如果它位于单独的文件中,则默认情况下它将无法正常工作,因为该文件不受ASP.NET处理管道的约束。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM