开发者

Show friendly message on ASP.NET Ajax error

开发者 https://www.devze.com 2023-02-02 19:47 出处:网络
You all know how annoying is this: I开发者_如何学JAVA do have a log system and the correct error is well explicit there, but I want to give a better message to the user.

You all know how annoying is this:

Show friendly message on ASP.NET Ajax error

I开发者_如何学JAVA do have a log system and the correct error is well explicit there, but I want to give a better message to the user.

I keep trying several ways but I'm using Telerik components and well jQuery and I ended up using both ASP.NET Ajax methods and jQuery, so I use

function pageLoad() {

    try {
        var manager = Sys.WebForms.PageRequestManager.getInstance();
        manager.add_endRequest(endRequest);
        manager.add_beginRequest(OnBeginRequest);
        manager
    }
    catch (err) {
        alert(err);
    }
}

as well

$(document).ready(function() { ... }

that alert(err) is never fired even upon OnClick events

what's the best approach to avoid this message errors and provide a cleaner way?

all this happens in <asp:UpdatePanel> as I use that when I didn't know better (3 years ago!) and I really don't want to mess up and build all again from scratch :(

Any help is greatly appreciated


Updated with more error windows after volpav solution

Show friendly message on ASP.NET Ajax error

Show friendly message on ASP.NET Ajax error


The error you're seeing there is not one that you can easily handle on the client-side. Since the PageRequestManager is unable to parse the response, you don't make it to the point of being able to handle endRequest and set_errorHandled().

What it's telling you is that something's mangling the response to your UpdatePanel's asynchronous postback. I wrote about one common cause of that a long time ago: http://encosia.com/2007/06/19/the-easiest-way-to-break-aspnet-ajax-pages/

As the error message states, another common cause are custom HttpModules that inject markup. GoDaddy uses (or used to) one to inject ads on free hosting accounts, which broke the UpdatePanel, for example.

If you inspect the response in something like Firebug or Fiddler, it should be obvious what that foreign content is. It's difficult for me to speculate about exactly what it may be without knowing more about your application.


You can use ScriptManager's AsynPostBackError event to do your custom error handling.


Scott explains precisely that in a blog post, so instead of hijacking this post check it out.

I used his method along with jQuery and a plugin called Noty and notifications looked way better.

Scott's post: http://weblogs.asp.net/davidbarkol/archive/2008/09/25/asynchronous-error-handling-change-in-asp-net-ajax-3-5.aspx

jQuery Noty Plugin: http://needim.github.io/noty/


var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_endRequest(function(sender, args) {
    var error = args.get_error();
    if(error != null && error.httpStatusCode != null) {
        // handle error
        args.set_errorHandled(true);
    
    }    
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号