<p>ValidateEvents is a great ASP.net function, but the Yellow Screen of Death is not so nice. I found a way how to handle the HttpRequestValidationException  gracefully <a href="http://www.romsteady.net/blog/2007/06/how-to-catch-httprequestvalidationexcep.html" rel="nofollow noreferrer">here</a>, but that does not work with ASP.net AJAX properly.</p>

<p>Basically, I got an UpdatePanel with a TextBox and a Button, and when the user types in HTML into the Textbox, a JavaScript Popup with a Error message saying not to modify the Response pops up.</p>

<p>So I wonder what is the best way to handle HttpRequestValidationException gracefully? For "normal" requests I would like to just display an error message, but when it's an AJAX Request i'd like to throw the request away and return something to indicate an error, so that my frontend page can react on it?</p>

[{'answer_id': 48831, 'author': 'Sara Chipps', 'author_id': 4140, 'author_profile': 'https://Stackoverflow.com/users/4140', 'pm_score': 0, 'selected': False, 'text': '<p>hmmmm, it seems you would need to find some sort of JavaScript to check for html input or a client side validator. </p>\n'}, {'answer_id': 50586, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 1, 'selected': False, 'text': '<p>That\'s what I would like to avoid if possible, but this seems to be much more complicated than expected.</p>\n\n<p>Normally, everyone advises using the AsyncPostBackError of the ScriptManager, but this does not work if called on the Global.asax. Unfortunately, as the HttpRequestValidationException is emitted by the runtime, it never enters my code and I cannot do much within the Application_Error.</p>\n\n<p>So yes, it needs to be indeed done in the JavaScript, I just hope there is a way to add a "hook" like the BeginRequestHandler-Function so that I don\'t have to "hack" Microsoft code. If I find a solution before someone else, i\'ll put it up here :-)</p>\n'}, {'answer_id': 50671, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 3, 'selected': True, 'text': '<p>Found it and <a href="http://www.stum.de/2008/09/08/gracefully-handling-httprequestvalidationexception-with-aspnet-ajax/" rel="nofollow noreferrer">blogged about it</a>. Basically, the EndRequestHandler and the args.set_errorHandled are our friends here.</p>\n\n<pre><code>&lt;script type="text/javascript" language="javascript"&gt;\nvar prm = Sys.WebForms.PageRequestManager.getInstance();\nprm.add_endRequest(EndRequestHandler);\n\nfunction EndRequestHandler(sender, args) {\n    if (args.get_error() != undefined)\n    {\n        var errorMessage;\n        if (args.get_response().get_statusCode() == \'200\')\n        {\n            errorMessage = args.get_error().message;\n        }\n        else\n        {\n            // Error occurred somewhere other than the server page.\n            errorMessage = \'An unspecified error occurred. \';\n        }\n        args.set_errorHandled(true);\n        $get(\'&lt;%= this.newsletterLabel.ClientID %&gt;\').innerHTML = errorMessage;\n    }\n}\n&lt;/script&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47864', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']