如何在ASP.NET Web应用程序中显示错误消息 [英] How to display an error message in an ASP.NET Web Application

查看:76
本文介绍了如何在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.net 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应用程序中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆