vb.net 将 Javascript 插入 ASP.NET 中的内容页面而不编译它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13358871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Javascript into content page in ASP.NET without compiling it
提问by CiccioMiami
I have an ASP.NET Web Formsapplication in VB.NET
我在VB.NET 中有一个ASP.NET Web 窗体应用程序
In my application I use a Master Pageand to insert Javascriptfor a specific page I use a ContentPlaceHolder.
在我的应用程序中,我使用 aMaster Page并为特定页面插入Javascript我使用ContentPlaceHolder.
I have a Javascriptwhere I use code nuggets and I inserted in my page like this:
我有一个Javascript,我在其中使用代码块,并像这样插入到我的页面中:
<asp:Content ID="Content4" ContentPlaceHolderID="javascript" runat="server">
<script language="javascript" type="text/javascript" >
function showErrors() {
var id = '<%=Request.QueryString("id") %>';
<%if (Request.QueryString("errors") == "true") {%>
var errorCode = '<%=Request.QueryString["errorCode"] %>';
var errorMessage = '<%=Request.QueryString["errorMessage"] %>';
<%} %>
}
</script>
</asp:Content>
The problem is that when I build the solution, also the Javascriptcode gets compiled and of course syntax errors are found. For instance one of the build errors is related to the ifstatement that does not have a matching End If(as it is supposed to be in VB.NET)
问题是,当我构建解决方案时,Javascript代码也会被编译,当然也会发现语法错误。例如,构建错误之一与if没有匹配的语句有关End If(因为它应该在 VB.NET 中)
How can I make the compiler understand that it has to skip the Javascript?
我怎样才能让编译器明白它必须跳过Javascript?
回答by Amiram Korach
You have to write the ifstatement in VBlanguage.
你必须if用VB语言写出声明。
<asp:Content ID="Content4" ContentPlaceHolderID="javascript" runat="server">
<script language="javascript" type="text/javascript" >
function showErrors() {
var id = '<%=Request.QueryString("id") %>';
<% If Request.QueryString("errors") = "true" Then %>
var errorCode = '<%=Request.QueryString["errorCode"] %>';
var errorMessage = '<%=Request.QueryString["errorMessage"] %>';
<% End If %>
}
</script>
</asp:Content>

