Usually after a web application is released, in order to provide users with a friendly interface and user experience, it will jump to a customized error page when an error occurs, instead of the detailed exception list exposed to the user by ASP.NET.
A simple error handling page can be set via web.config
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
If you want to present the cause of the error programmatically, you can do this through the Page_Error event.
Another way can be achieved through Global.asax. I think this way is more convenient. In addition, if it can be combined with a separate more friendly page, it will look more comfortable.
Global.asax (error logging if needed) void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string error = "Exception page: " + Request.Url.ToString() + "<br>";
error += "Exception message: " + objErr.Message + "<br>";
Server.ClearError();
Application["error"] = error;
Response.Redirect("~/ErrorPage/ErrorPage.aspx");
}
ErrorPage.aspx
protected void Page_Load(object sender, EventArgs e)
{
ErrorMessageLabel.Text = Application["error"].ToString();
}When end users use the application, they may not want to know the cause of the error. At this time, we can use check boxes to determine whether to display the cause of the error. You can put the Label in a div and then use the checkbox to decide whether to render the div
<script language="javascript" type="text/javascript">
<!--
function CheckError_onclick() {
var chk = document.getElementById("CheckError");
var divError = document.getElementById("errorMsg");
if(chk.checked)
{
divError.style.display = "inline";
}
else
{
divError.style.display = "none";
}
}
// -->
</script>
http://www.cnblogs.com/EasyLive2006/archive/2007/01/07/613922.html