I'm working on web forms again and I'm trying to display the exception message without going to the yellow screen of death (YSD). I don't want to use those try-catch
or go for code level exception handling as I want it to have a global exception handler or at least a page-level exception handler.
So far, what I did is to set the customError
mode to Off
in order not to display the error in the YSD.
I used this link as my reference. ASP.NET Error Handling
So in my page, I added a Label to display the message in my html page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="col-lg-4 col-lg-offset-4">
<div class="form-group">
<label class=" control-label">Username: </label>
<asp:TextBox runat="server" ID="Username" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<label class="control-label">Password: </label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
</div>
<div class="form-group text-center">
<asp:LinkButton ID="LoginButton" CssClass="btn btn-casa" runat="server" OnClick="LoginButton_click">Login</asp:LinkButton>
<asp:LinkButton ID="Clear" runat="server" CssClass="btn btn-casa-outline">Clear</asp:LinkButton>
<span>New user? </span><asp:HyperLink runat="server" CssClass="btn-link" NavigateUrl="~/Pages/Admin/Register.aspx">Register</asp:HyperLink>
</div>
</div>
<div>
<asp:Label ID="Error" runat="server" Visible="false" CssClass="error-label"></asp:Label>
</div>
</asp:Content>
And this is what I did in the code behind.
protected void Page_Error()
{
Exception ex = Server.GetLastError();
// Handle specific exception.
Error.Text = ex.Message;
Error.Visible = true;
// Clear the error from the server.
Server.ClearError();
}
However what happens is that once an exception has occurred, it will just show a white screen instead of keeping the page itself and displaying the error message in the label.