This one took me a while. I was playing around with putting script into my web site's feed back form, trying to capture RequestValidation exceptions. I wasn't having much luck.
The symptom of my initial attempt at handling the error was that no matter what sort of code I included in the Page_Error method, I always viewed the .Net yellow screen of embarrassment when I purposely introduced a <script> tag into the feed back text fields.
It turns out, you have to complete the request in the Page_Error method. The bad news is, your page processing ends. BOOM! So things like style sheets and master page processing will not happen.
You can capture the error in the Application_Error method of global.asax This lets you do a few more things, but basically, the request must be handled in the Application_Error.
What all the verbiage means is that a Request.End() call must be made before your error handing method returns, or else you'll get the yellow screen of embarrassment.
Here's an example of how I handled the exception in my feed back form code behind:
string btnGoBack = "<br/><br/><input id=\"btnBack\" type=\"button\" value=\"Back\" onclick=\"history.back()\"/>";
protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpRequestValidationException)
{
string resMsg = "<html><body><span style=\"font-size: 14pt; color: red\">" +
".Net has detected material in your email that is not allowed.<br />" +
btnGoBack +
"<br /></span></body></html>";
Response.Write(resMsg);
Response.StatusCode = 200;
Response.End();
}
}