如何在ASP.NET Web应用程序中显示错误消息

编程入门 行业动态 更新时间:2024-10-14 04:24:32
本文介绍了如何在ASP.NET Web应用程序中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ASP.NET Web应用程序,我想知道引发异常时如何显示错误消息框。

I have an ASP.NET web application, and I wanted to know how I could display an error message box when an exception is thrown.

例如

try { do something } catch { messagebox.write("error"); //[This isn't the correct syntax, just what I want to achieve] }

[消息框显示错误]

[The message box shows the error]

谢谢

重复的 如何在Web应用程序中显示错误消息框asp c#

推荐答案

大致上可以这样做:

try { //do something } catch (Exception ex) { string script = "<script>alert('" + ex.Message + "');</script>"; if (!Page.IsStartupScriptRegistered("myErrorScript")) { Page.ClientScript.RegisterStartupScript("myErrorScript", script); } }

但是我建议您定义自定义异常并抛出它在您需要的任何地方。在您的页面上捕获此自定义异常并注册您的消息框脚本。

But I recommend you to define your custom Exception and throw it anywhere you need. At your page catch this custom exception and register your message box script.

更多推荐

如何在ASP.NET Web应用程序中显示错误消息

本文发布于:2023-11-15 15:57:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   错误   消息   如何在   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!