views:

345

answers:

2

Normally I just redirect to a custom error page in on the Application_Error event, but I have a specific error for which I'd like to display an alert message while the user is still on the page which triggers the error. How can I make this happen?

I'm open to a modalpopup or any other type of error message, I just want to ensure the user stays on the page where they encounter the error.

Thank for any ideas.

This is in reference to this thread: http://stackoverflow.com/questions/2464341/a-potentially-dangerous-request-form-value-was-detected-dealing-with-these-error

Here is the code I'm currently using:

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
A: 

If you know where the error occurs, wrap it in a try-catch block and handle it in the code behind page load.

If you want a Javascript styled modal pop-up error, in the catch, write out a flag value / error message to a hidden variable on the page and handle it in the html document load.

Doobi
please see my comment above.
Albert
Hmm, then here's another thought - in the Global.asax, in the "on request" handler capture the requested URL. When you trap the error redirect the request back to the page, but inject code to cause a pop-up.
Doobi
Are you using a base page that your pages inherit from? If so put the code to handle this in there
bechbd
^ Actually I do use a master page, I might try that later. Thanks
Albert
A: 

I couldn't figure out an easy way to do this, so I just made new error page for these types of errors, and Redirect to that page when that specific error is caught in the Application_Error event (as my code in the first post indicates).

Albert