代码之家  ›  专栏  ›  技术社区  ›  Albert

显示来自global.asax的警报消息框(在应用程序错误事件中)

  •  1
  • Albert  · 技术社区  · 16 年前

    通常,我只是重定向到应用程序错误事件中的自定义错误页,但我有一个特定的错误,当用户仍在触发错误的页上时,我希望显示一条警告消息。我怎样才能做到?

    我打开了一个modalpopup或任何其他类型的错误消息,我只想确保用户停留在他们遇到错误的页面上。

    谢谢你的意见。

    这是对这个线程的引用: A potentially dangerous Request.Form value was detected: Dealing with these errors proactively, or after the fact

    以下是我当前使用的代码:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        'Code that runs when an unhandled error occurs
        Try
            Dim err As Exception = Server.GetLastError
            If err.Message IsNot Nothing Then
                If err.Message = "The client disconnected." Then
                    Dim LogError As New LogError(Server.GetLastError, Session, Request)
                    LogError.LogError()
                    Response.Redirect("~/TimeoutPage.aspx?id=2")
                ElseIf err.Message.Contains("dangerous Request.Form value") Then
                    'Response.Redirect("~/MyInputErrorPage.aspx")
                'Instead the above redirect, I'd like to show the user an alertbox or something similar to explain the error to them
                Else
                    Dim LogError As New LogError(Server.GetLastError, Session, Request)
                    LogError.LogError()
                    Response.Redirect("~/MyErrorPage.aspx")
                End If
            End If
        Catch ex As Exception
        End Try
    
    2 回复  |  直到 16 年前
        1
  •  0
  •   Doobi    16 年前

    如果知道错误发生在哪里,请将其包装在try catch块中,并在代码隐藏页加载中处理。

    如果您想要一个javascript样式的模式弹出错误,在catch中,向页面上的隐藏变量写出一个标志值/错误消息,并在html文档加载中处理它。

        2
  •  0
  •   Albert    16 年前

    我想不出一个简单的方法来实现这一点,所以我只是为这些类型的错误创建了一个新的错误页面,并在应用程序错误事件中捕获到特定错误时重定向到该页面(如我在第一篇文章中的代码所示)。