Saturday, August 16, 2008

Disabling Ajax JavaScript error message

By default, if an exception is thrown in the asynchronized code, ScriptManager shows the exception in a JavaScript Alert window. Although this behavior is required some times, mostly for security purposes programmer wants to hide the run-time errors from the end users even if it does not make any clear sense to them. This is especially important for public web sites which can be exposed to all types of threats.

ScriptManager exposes an event called AsyncPostBackError that is fired every time there is an exception thrown during the execution of partial postback code. We can use this event in order to stop the exception from reaching the client script by re-throwing it; therefore, it can be captured by general web site error capturing mechanism.

protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
    {
        throw (e.Exception);
    }

No comments: