8.2. Handling Errors at the Page Level
You want to trap any error that occurs on a page and, using a page-level event handler, redirect the user to another page that displays the information about the problem.
Add code to the
Page_Error event handler of the page to set the
ErrorPage property of that page to the URL you want to display to the user when an error occurs.
In the code-behind for the page, use the .NET language of your choice to:
In the event handler, get a reference to the last error that occurred using the
ErrorPageproperty of the
Pageobject to the URL of the page you want displayed after the error, adding
querystringparameters to pass error information to the page.
Page_Error event of the ASP.NET
Page object is raised any time an unhandled error occurs in a page. The first action required in the event handler is to get a reference to the last error. After getting the reference, the code should perform the required logging, notifications, etc. See Recipe 8.3 for an example of writing to the event log.
ASP.NET provides you with the ability to redirect the user to another page when an error occurs. To use this feature, set the
ErrorPage property of the
Page object to the URL of the page you want the user to see. You ...