The simple error handling page can be set through web. config.
Copy codeThe Code is as follows:
<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>
If you want to program the cause of the error, you can use the Page_Error event.
The other method can be implemented through Global. asax. I think this method is more convenient. In addition, if you can combine a separate and more friendly page, it seems more comfortable:
Global. asax (you can record the error log if necessary)
Copy codeThe Code is as follows:
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. aspx ");
}
ErrorPage. aspx
Protected void Page_Load (object sender, EventArgs e)
{
ErrorMessageLabel. Text = Application ["error"]. ToString ();
}
When end users use applications, they may not want to know the cause of the error. In this case, we can use the check box to check whether the cause of the error is displayed. You can place the Label in a div and use the check box to determine whether to render the div.
Copy codeThe Code is as follows:
<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>