Can I ask about why my output for PageError.aspx is not same as correct output below, please?:
The correct output for PageError.aspx after I entered two inputs such as 7 and 0 by using the pageerror method only (cannot use try catch method):
Sorry. One Error is encountered in this page: Attempted to divide by zero.
I still cannot get this correct output as mentioned above after I Google several times and sample source code, the PageError.aspx output still display the input for perform division with 2 blanks. Can you all explain my errors, please?
What I have tried:
I paste some these problems code here as below:
For PageError.aspx,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageError.aspx.cs" Inherits="WebApplication1.PageError" Trace="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Error Handling</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<p style="width:60%; padding:30px 20px 30px 20px; border:solid 3px black">
<asp:TextBox ID="TextBox1" runat="server" Height="43px" Width="66px"
Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
<img alt="" src="images/Divide.jpg" style="height: 39px; width: 65px" />
<asp:TextBox ID="TextBox2" runat="server" Height="43px" Width="66px"
Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="X-Large"></asp:TextBox>
<asp:ImageButton ID="EqualButton" runat="server" Height="44px"
ImageUrl="~/images/EqualButton.jpg" Width="50px"
onclick="EqualButton_Click" />
<asp:Label ID="Label1" runat="server" Font-Bold="True"
Font-Names="Comic Sans MS" Font-Size="XX-Large" ForeColor="Blue" Text="?"></asp:Label>
<br />
<asp:Label ID="lblMessage" runat="server" Text="" Font-Names="Arial" ForeColor="Red"></asp:Label>
</p>
</div>
</form>
</body></html>
For PageError.aspx.cs,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class PageError : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EqualButton_Click(object sender, ImageClickEventArgs e)
{
double dblAns = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(TextBox2.Text);
Label1.Text = dblAns.ToString();
}
void Page_Error()
{
Response.Write("Sorry <br /><br />" + "One error is encountered in this page: " + Server.GetLastError().Message + "");
Server.ClearError();
}
}
}
For Web Config,
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https:
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
<!--<customErrors mode="On" defaultRedirect="ErrorPages/PageError.htm">
<error statusCode="404" redirect="ErrorPages/FileNotFound.htm"/>
</customErrors>-->
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="File">
<clear/>
<error statusCode="404" path="ErrorPages\FileNotFound.htm"/>
</httpErrors>
</system.webServer>
</configuration>