简体   繁体   English

Response.Write Javascript警报时发生错误

[英]Error When Response.Write Javascript Alert

I'm trying to do a Response.Write to pop up a Javascript error message when the script reaches my catch block in my ASPX page (not in a code behind or a class). 当脚本到达ASPX页面中的catch块(而不是背后的代码或类)时,我试图做一个Response.Write来弹出Javascript错误消息。

Here is the code: 这是代码:

catch (Exception ex)
{
    Response.Write("<script language=javascript>alert('ERROR');</script>");

    //System.Windows.Forms.MessageBox.Show(ex.ToString());
}

The error message I get is: 我收到的错误消息是:

} expected

No idea why it's telling me I need a } . 不知道为什么它告诉我我需要} Everything has been closed, when I remove/comment out that line and put something else, I don't get any errors so obviously, it's not like I'm missing a closing } somewhere... 一切都已关闭,当我删除/注释该行并放入其他内容时,很明显我没有收到任何错误,这就像我在某个地方缺少}

Why is this happening?? 为什么会这样?

Problem is you probably have this statement enveloped in 问题是您可能将此语句包含在其中

<script type="text/c#">
</script>

So the 所以

</script>

in statement below is taken as ending the C# script block 以下语句中的in作为结束C#脚本块

Response.Write("<script language=javascript>alert('ERROR');</script>");

I see two options - move this logic to codebehind or escape the script to something like 我看到两个选择-将此逻辑移到代码后方或将脚本转义为类似

Response.Write("<script language=javascript>alert('ERROR');" + "<" + "/" + "script>");

If I were you, I would use RegisterScriptBlock . 如果我是你,我将使用RegisterScriptBlock please read the text and the example. 请阅读正文和示例。 I am certainly sure that is what you need 我当然确定那是您所需要的

我认为您在语言属性附近缺少单引号,请尝试以下操作:

Response.Write("<script language='javascript'>alert('ERROR');</script>");

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

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