Problem: While using Ajax with asp.net, if we get crash on any event which was fired by a control in a update panel we do not get any yellow screen or error page. And end user does not get any message or result.
Solution:
Step-1: Add OnAsyncPostBackError event to the script manager and set AllowCustomErrorsRedirect property to true.
<asp:scriptmanager allowcustomerrorsredirect="true" id="smBody" onasyncpostbackerror="smBody_AsyncPostBackError" runat="server"></asp:scriptmanager>
Step-2: Add the following script after the script manager(Otherwise you will get javascript error on the page).
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
</script>
Step-3: Add the following script to the page:
<script type="text/javascript">
function EndRequestHandler(sender, args) {
if (args.get_error() != undefined) {
var Error = "Error Code: " + args.get_response().get_statusCode() + "\nError Message: " +
args.get_error().message;
alert(Error);
args.set_errorHandled(true);
//Uncomment the following line to refresh the window after alert the error message.
//window.location = window.location;
}
}
</script>
Step-4: Write the script-manager's AsyncPostBackError event:
protected void smBody_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
smBody.AsyncPostBackErrorMessage = e.Exception.Message;
}
Now all the postback errors will be displayed in alert message and after that the page will be reloaded :-).
Thanks. :-)
No comments:
Post a Comment