With Ajax Update panel, ScriptManager.RegisterStartUpScript is not working if placed in page_onerror
I have a requirement to show user an alert box when there is some exception happened at page level (For different exceptions message is different). In page I had two update panels and trigger based updates. And this functionality is common across pages. So I created a base page and tried to handle exception i开发者_高级运维n page_onerror event. And I am using ScriptManager.RegisterStartupScript(...) to show alert for user. If I use same code instead of throwing error in catch block, it is working fine. But if I throw exception and handling in page_error method, it is not working...
(As I am using update panels, I cannot use Response.Write for the same.)
Please help me.
Thanks in advance.
The page life cycle is slightly different in Async Postbacks - are you certain if Page_OnError
is getting fired? If yes then perhaps the event fired so late that script manager has already rendered registered scripts into the output.
You should perhaps try ScriptManager.AsyncPostBackError event. If all you want to do is to show custom error alert then setting ScriptManager.AsyncPostBackErrorMessage property should do the trick.
I typically use ScriptManager
in the master page and handles AsyncPostBackError
event in the code behind to send back some custom exception. On java-script side, I use end request handler to show custom alert box. Check the example code from above MSDN link:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args)
{
if (args.get_error() != undefined && args.get_error().httpStatusCode == '500')
{
var errorMessage = args.get_error().message
args.set_errorHandled(true);
ToggleAlertDiv('visible');
$get(messageElem).innerHTML = '"' +
errorMessage + '" ' + errorMessageAdditional;
}
}
精彩评论