当出现错误时调用一个javascript函数——ASP.NET。

时间:2022-06-19 15:58:46

I have overridden OnError method in the page:

我在页面上覆盖了错误方法:

public string script;

    protected override void OnError(EventArgs e)
    {
        script = "alert('error');";
    }

In the aspx, I have this:

在aspx,我有这个:

<script type="text/javascript">
$(document).ready(function(){
        <%=script  %>;
    });
</script>

I am simulating an error in Page_Load, but its not firing the alert. If I switch the code from OnError to OnLoad, it works great.

我正在模拟Page_Load中的错误,但是它没有触发警报。如果我将代码从OnError切换到OnLoad,它就会运行得很好。

Thoughts?

想法吗?

4 个解决方案

#1


0  

The fact that you're throwing an Exception and not handling it in the page with a try/catch means that the page never finishes rendering. Not only are you not getting the output of your script string, you'll probably find you're not getting the $(document).ready() or anything else either as the page isn't rendering.

事实上,您抛出一个异常,并没有在页面中使用try/catch来处理它,这意味着该页面从未完成呈现。您不仅没有得到脚本字符串的输出,还可能发现您没有得到$(document).ready()或其他任何内容,因为页面没有呈现。

If you handle the exception like this then the page rendering will complete and your message will display.

如果您处理这样的异常,那么页面呈现将完成,您的消息将显示出来。

try
{
    throw new Exception();
}
catch(Exception ex)
{
    // log ex.ToString() here
    script = "alert('error')";
    // decide what to do here - carry on or return
}

In real terms you should try to avoid catching Exception, always try to anticipate which specific exceptions you expect and handle them accordingly. In fact, always try to avoid cases where an Exception can be thrown in the first place - Exceptions should be thrown in exceptional circumstances.

在实际中,您应该尽量避免捕获异常,总是试图预测您期望的特定异常并相应地处理它们。实际上,总是尽量避免在第一个地方抛出异常——异常情况下应该抛出异常。

Have a read through this CodeProject article that discusses some best practices regarding exception handling.

请阅读本CodeProject文章,讨论有关异常处理的一些最佳实践。

#2


1  

When you override OnError, you need to clear the error so you mark it as handled, if you don't do this then it will bubble up to Application_Error, trashing any content your page may have rendered..

当您重写OnError时,您需要清除错误,以便将其标记为已处理的错误,如果您不这样做,那么它将冒冒进到Application_Error,对您的页面可能呈现的任何内容进行破坏。

Try using Server.ClearError

试着用Server.ClearError

protected override void OnError(EventArgs e)     
{         
   script = "alert('error');";
   Server.ClearError(); //Prevent the error from propagating     
}

Looking in reflector you can see this..

你可以看到反射器。

try
{
    this.Context.TempError = e;
    this.OnError(EventArgs.Empty);
    if (this.Context.TempError == null)
    {
        return true;
    }
}
finally
{
    this.Context.TempError = null;
}

Server.ClearError clears that TempError

服务器。ClearError清除,TempError

public void ClearError()
{
    if (this._tempError != null)
    {
        this._tempError = null;
    }
   ...other code..
 }

#3


0  

What you are doing would result in code like this in the output page:

您所做的将会在输出页面中产生这样的代码:

<script type="text/javascript">
$(document).ready(function(){
   <script language='javascript' type='text/javascript'>alert('error')</script>;
});
</script>

which is obviously wrong. You need to change the script variable to:

这显然是错误的。您需要将脚本变量更改为:

script = @"alert('error')";

#4


0  

Because the page isn't erroring and so the OnError event isn't being fired.

因为页面不会出错,所以OnError事件不会被触发。

Or are you forcing the error and the alert still isn't showing?

还是你在强迫错误,警告仍然没有显示?

By the way, take out the tags in the script insertion, and just have the alert - you already have the defined in your ASPX.

顺便说一下,在脚本插入中去掉标记,并且只有警告——您已经在ASPX中定义了这个标记。

Update:

更新:

It's likely to do with the hierarchy of calls, the document.ready() is being called BEFORE the OnError is happening.

它可能与调用的层次结构有关,document.ready()在OnError发生之前被调用。

Try putting the whole thing into a literal, such as:

试着把所有的事情都写下来,比如:

<asp:Literal runat="server" ID="JScript" />

    protected override void OnError(EventArgs e)
        {
            JScript.Text = "<script type=\"text/javascript\">alert('error');</script>";
        }

#1


0  

The fact that you're throwing an Exception and not handling it in the page with a try/catch means that the page never finishes rendering. Not only are you not getting the output of your script string, you'll probably find you're not getting the $(document).ready() or anything else either as the page isn't rendering.

事实上,您抛出一个异常,并没有在页面中使用try/catch来处理它,这意味着该页面从未完成呈现。您不仅没有得到脚本字符串的输出,还可能发现您没有得到$(document).ready()或其他任何内容,因为页面没有呈现。

If you handle the exception like this then the page rendering will complete and your message will display.

如果您处理这样的异常,那么页面呈现将完成,您的消息将显示出来。

try
{
    throw new Exception();
}
catch(Exception ex)
{
    // log ex.ToString() here
    script = "alert('error')";
    // decide what to do here - carry on or return
}

In real terms you should try to avoid catching Exception, always try to anticipate which specific exceptions you expect and handle them accordingly. In fact, always try to avoid cases where an Exception can be thrown in the first place - Exceptions should be thrown in exceptional circumstances.

在实际中,您应该尽量避免捕获异常,总是试图预测您期望的特定异常并相应地处理它们。实际上,总是尽量避免在第一个地方抛出异常——异常情况下应该抛出异常。

Have a read through this CodeProject article that discusses some best practices regarding exception handling.

请阅读本CodeProject文章,讨论有关异常处理的一些最佳实践。

#2


1  

When you override OnError, you need to clear the error so you mark it as handled, if you don't do this then it will bubble up to Application_Error, trashing any content your page may have rendered..

当您重写OnError时,您需要清除错误,以便将其标记为已处理的错误,如果您不这样做,那么它将冒冒进到Application_Error,对您的页面可能呈现的任何内容进行破坏。

Try using Server.ClearError

试着用Server.ClearError

protected override void OnError(EventArgs e)     
{         
   script = "alert('error');";
   Server.ClearError(); //Prevent the error from propagating     
}

Looking in reflector you can see this..

你可以看到反射器。

try
{
    this.Context.TempError = e;
    this.OnError(EventArgs.Empty);
    if (this.Context.TempError == null)
    {
        return true;
    }
}
finally
{
    this.Context.TempError = null;
}

Server.ClearError clears that TempError

服务器。ClearError清除,TempError

public void ClearError()
{
    if (this._tempError != null)
    {
        this._tempError = null;
    }
   ...other code..
 }

#3


0  

What you are doing would result in code like this in the output page:

您所做的将会在输出页面中产生这样的代码:

<script type="text/javascript">
$(document).ready(function(){
   <script language='javascript' type='text/javascript'>alert('error')</script>;
});
</script>

which is obviously wrong. You need to change the script variable to:

这显然是错误的。您需要将脚本变量更改为:

script = @"alert('error')";

#4


0  

Because the page isn't erroring and so the OnError event isn't being fired.

因为页面不会出错,所以OnError事件不会被触发。

Or are you forcing the error and the alert still isn't showing?

还是你在强迫错误,警告仍然没有显示?

By the way, take out the tags in the script insertion, and just have the alert - you already have the defined in your ASPX.

顺便说一下,在脚本插入中去掉标记,并且只有警告——您已经在ASPX中定义了这个标记。

Update:

更新:

It's likely to do with the hierarchy of calls, the document.ready() is being called BEFORE the OnError is happening.

它可能与调用的层次结构有关,document.ready()在OnError发生之前被调用。

Try putting the whole thing into a literal, such as:

试着把所有的事情都写下来,比如:

<asp:Literal runat="server" ID="JScript" />

    protected override void OnError(EventArgs e)
        {
            JScript.Text = "<script type=\"text/javascript\">alert('error');</script>";
        }