I think I understand what you want to do, but I'm afraid it is impossible. When your ASP.NET page performs a postback, a new thread is created on the server to process the request. Before the life cycle of your page even begins, offensive XSS will be detected and an exception will be thrown. After this exception is thrown, you are "evicted" from the life cycle of the ASP.NET page, and there is no way to re-enter it. At this stage, the only thing you can do on the client side is to display an error or redirect to the error page.
What you seem to want to do is catch the exception, write it somewhere on the page and continue the life cycle of the ASP.NET page (i.e. restore the control tree, restore the view state, call event handlers, etc. ) The problem is that when you get an unhandled exception, you no longer have access to the life cycle of the ASP.NET page. In this particular case, there is no place to block try / catch, because an exception is thrown from the ASP.NET lifecycle before calling its own code.
I know that you said you didn’t want to rely on Javascript, but in this case I think using Javascript is the only way to get the behavior you need. You can still keep the server side validation, in case your users turn off Javascript or enter some data that your Javascript does not process.
source share