1

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.

Martin
  • 31
  • 3
Musikero31
  • 3,115
  • 6
  • 39
  • 60

1 Answers1

0

By the time you are in Page_Error the page no longer exists

A page-level handler returns the user to the page where the error occurred, but because instances of controls are not maintained, there will no longer be anything on the page.

I think your best option would be a try catch

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling#page-level-error-event-handling

NicoTek
  • 1,127
  • 1
  • 14
  • 34
  • Sorry, I updated my inquiry. What I want is that when there's an exception it will stay on the page and just display the error message in the label. That's why I have the error label control. – Musikero31 May 26 '21 at 13:15
  • updated the answer, the webforms lifecycle is tricky like that. – NicoTek May 26 '21 at 13:36