Wednesday, December 28, 2022

Exception handling update panel script manager - ASP.NET C# - onAsyncPostBackError

 catch (Exception ex)
    {
        string message = string.Format("Message: {0}\\n\\n", ex.Message);
        message += string.Format("StackTrace: {0}\\n\\n", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
        message += string.Format("Source: {0}\\n\\n", ex.Source.Replace(Environment.NewLine, string.Empty));
        message += string.Format("TargetSite: {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
        ClientScript.RegisterStartupScript(this.GetType(), "alert""alert(\"" + message + "\");"true);
    }


 reference:- https://www.aspsnippets.com/Articles/Display-Exception-Error-Message-details-using-JavaScript-Alert-MessageBox-in-ASPNet.aspx


or


<script type="text/javascript">

              //function pageLoad() {

       //    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

       //}


       ////Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

       //function EndRequestHandler(sender, args) {

       //    if (args.get_error() != undefined) {

       //        args.set_errorHandled(true);

       //        alert(args.get_error());

       //    }

       //}

   </script>


or

 <script type="text/javascript">

       function pageLoad() {

           Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {

               if (args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerServerErrorException') {

                   args.set_errorHandled(true);

                   alert(args.get_error());

               }

           });

       }

</script>

 protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)

        {

            ScriptManager1.AsyncPostBackErrorMessage = "An error occurred during the request: " + e.Exception.StackTrace;

}



or


<script type="text/javascript">

       function pageLoad() {

           Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

       }

        

       function EndRequestHandler(sender, args) {

           if (args.get_error() != undefined) {

               var errorMessage = args.get_error().message;

               args.set_errorHandled(true);

               //alert(errorMessage);

               $('.modal-body').html(args.get_error());

               $('#myModal').modal('show');

           }

       }

   </script>

 <!-- Modal -->

          <div class="modal fade" id="myModal" role="dialog">

            <div class="modal-dialog modal-sm">

              <div class="modal-content">

                <div class="modal-header">

                  <button type="button" class="close" data-dismiss="modal">&times;</button>

                  <h4 class="modal-title">Error</h4>

                </div>

                <div class="modal-body">

                  <p>This is a small modal.</p>

                </div>

                <div class="modal-footer">

                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                </div>

              </div>

            </div>

          </div>

Note:- alert will not working fine in EndRequestHandler(), it will show many times, you have to click ok many times when get error from server.


or


function EndRequestHandler(sender, args) { if (args.get_error() != undefined && args.get_error().httpStatusCode == '500') { var errorMessage = args.get_error().message args.set_errorHandled(true); ToggleAlertDiv('visible'); $get(messageElem).innerHTML = '"' + errorMessage + '" ' + errorMessageAdditional; } }

reference:- https://www.c-sharpcorner.com/uploadfile/sridhar_subra/handling-ajax-errors/

No comments:

Post a Comment