ErrorHandlingExampleCS.aspx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5. protected void ErrorProcessClick_Handler(object sender, EventArgs e)
  6. {
  7. // This handler demonstrates an error condition. In this example
  8. // the server error gets intercepted on the client and an alert is shown.
  9. Exception exc = new ArgumentException();
  10. exc.Data["GUID"] = Guid.NewGuid().ToString().Replace("-"," - ");
  11. throw exc;
  12. }
  13. protected void SuccessProcessClick_Handler(object sender, EventArgs e)
  14. {
  15. // This handler demonstrates no server side exception.
  16. UpdatePanelMessage.Text = "The asynchronous postback completed successfully.";
  17. }
  18. protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
  19. {
  20. if (e.Exception.Data["GUID"] != null)
  21. {
  22. ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message +
  23. " When reporting this error use the following ID: " +
  24. e.Exception.Data["GUID"].ToString();
  25. }
  26. else
  27. {
  28. ScriptManager1.AsyncPostBackErrorMessage =
  29. "The server could not process the request.";
  30. }
  31. }
  32. </script>
  33. <html xmlns="http://www.w3.org/1999/xhtml">
  34. <head runat="server">
  35. <title>PageRequestManager endRequestEventArgs Example</title>
  36. <style type="text/css">
  37. body {
  38. font-family: Tahoma;
  39. }
  40. #AlertDiv{
  41. left: 40%; top: 40%;
  42. position: absolute; width: 200px;
  43. padding: 12px;
  44. border: #000000 1px solid;
  45. background-color: white;
  46. text-align: left;
  47. visibility: hidden;
  48. z-index: 99;
  49. }
  50. #AlertButtons{
  51. position: absolute;
  52. right: 5%;
  53. bottom: 5%;
  54. }
  55. </style>
  56. </head>
  57. <body id="bodytag">
  58. <form id="form1" runat="server">
  59. <div>
  60. <asp:ScriptManager ID="ScriptManager1"
  61. OnAsyncPostBackError="ScriptManager1_AsyncPostBackError"
  62. runat="server" />
  63. <script type="text/javascript" language="javascript">
  64. var divElem = 'AlertDiv';
  65. var messageElem = 'AlertMessage';
  66. var bodyTag = 'bodytag';
  67. Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  68. function ToggleAlertDiv(visString)
  69. {
  70. if (visString == 'hidden')
  71. {
  72. $get(bodyTag).style.backgroundColor = 'white';
  73. }
  74. else
  75. {
  76. $get(bodyTag).style.backgroundColor = 'gray';
  77. }
  78. var adiv = $get(divElem);
  79. adiv.style.visibility = visString;
  80. }
  81. function ClearErrorState() {
  82. $get(messageElem).innerHTML = '';
  83. ToggleAlertDiv('hidden');
  84. }
  85. function EndRequestHandler(sender, args)
  86. {
  87. if (args.get_error() != undefined)
  88. {
  89. var errorMessage = args.get_error().message;
  90. args.set_errorHandled(true);
  91. ToggleAlertDiv('visible');
  92. $get(messageElem).innerHTML = errorMessage;
  93. }
  94. }
  95. </script>
  96. <asp:UpdatePanel runat="Server" UpdateMode="Conditional" ID="UpdatePanel1">
  97. <ContentTemplate>
  98. <asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
  99. <asp:Label ID="UpdatePanelMessage" runat="server" />
  100. <br />
  101. Last update:
  102. <%= DateTime.Now.ToString() %>
  103. .
  104. <br />
  105. <asp:Button runat="server" ID="Button1" Text="Submit Successful Async Postback"
  106. OnClick="SuccessProcessClick_Handler" OnClientClick="ClearErrorState()" />
  107. <asp:Button runat="server" ID="Button2" Text="Submit Async Postback With Error"
  108. OnClick="ErrorProcessClick_Handler" OnClientClick="ClearErrorState()" />
  109. <br />
  110. </asp:Panel>
  111. </ContentTemplate>
  112. </asp:UpdatePanel>
  113. <div id="AlertDiv">
  114. <div id="AlertMessage">
  115. </div>
  116. <br />
  117. <div id="AlertButtons" >
  118. <input id="OKButton" type="button" value="OK"
  119. runat="server" onclick="ClearErrorState()" />
  120. </div>
  121. </div>
  122. </div>
  123. </form>
  124. </body>
  125. </html>