default.aspx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <script runat="server">
  4. protected void Button1_Click(object sender, EventArgs e)
  5. {
  6. try
  7. {
  8. int a = Int32.Parse(TextBox1.Text);
  9. int b = Int32.Parse(TextBox2.Text);
  10. int res = a / b;
  11. Label1.Text = res.ToString();
  12. }
  13. catch (Exception ex)
  14. {
  15. if (TextBox1.Text.Length > 0 && TextBox2.Text.Length > 0)
  16. {
  17. ex.Data["ExtraInfo"] = " You can't divide " +
  18. TextBox1.Text + " by " + TextBox2.Text + ".";
  19. }
  20. throw ex;
  21. }
  22. }
  23. protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
  24. {
  25. if (e.Exception.Data["ExtraInfo"] != null)
  26. {
  27. ScriptManager1.AsyncPostBackErrorMessage =
  28. e.Exception.Message +
  29. e.Exception.Data["ExtraInfo"].ToString();
  30. }
  31. else
  32. {
  33. ScriptManager1.AsyncPostBackErrorMessage =
  34. "An unspecified error occurred.";
  35. }
  36. }
  37. </script>
  38. <html xmlns="http://www.w3.org/1999/xhtml">
  39. <head id="Head1" runat="server">
  40. <title>UpdatePanel Error Handling Example</title>
  41. <style type="text/css">
  42. #UpdatePanel1 {
  43. width: 200px; height: 50px;
  44. border: solid 1px gray;
  45. }
  46. #AlertDiv{
  47. left: 40%; top: 40%;
  48. position: absolute; width: 200px;
  49. padding: 12px;
  50. border: #000000 1px solid;
  51. background-color: white;
  52. text-align: left;
  53. visibility: hidden;
  54. z-index: 99;
  55. }
  56. #AlertButtons{
  57. position: absolute; right: 5%; bottom: 5%;
  58. }
  59. </style>
  60. </head>
  61. <body id="bodytag">
  62. <form id="form1" runat="server">
  63. <div>
  64. <asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
  65. </asp:ScriptManager>
  66. <script type="text/javascript" language="javascript">
  67. var divElem = 'AlertDiv';
  68. var messageElem = 'AlertMessage';
  69. var bodyTag = 'bodytag';
  70. Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  71. function ToggleAlertDiv(visString)
  72. {
  73. if (visString == 'hidden')
  74. {
  75. $get(bodyTag).style.backgroundColor = 'white';
  76. }
  77. else
  78. {
  79. $get(bodyTag).style.backgroundColor = 'gray';
  80. }
  81. var adiv = $get(divElem);
  82. adiv.style.visibility = visString;
  83. }
  84. function ClearErrorState() {
  85. $get(messageElem).innerHTML = '';
  86. ToggleAlertDiv('hidden');
  87. }
  88. function EndRequestHandler(sender, args)
  89. {
  90. if (args.get_error() != undefined)
  91. {
  92. var errorMessage;
  93. if (args.get_response().get_statusCode() == '200')
  94. {
  95. errorMessage = args.get_error().message;
  96. }
  97. else
  98. {
  99. // Error occurred somewhere other than the server page.
  100. errorMessage = 'An unspecified error occurred. ';
  101. }
  102. args.set_errorHandled(true);
  103. ToggleAlertDiv('visible');
  104. $get(messageElem).innerHTML = errorMessage;
  105. }
  106. }
  107. </script>
  108. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  109. <ContentTemplate>
  110. <asp:TextBox ID="TextBox1" runat="server" Width="39px"></asp:TextBox>
  111. /
  112. <asp:TextBox ID="TextBox2" runat="server" Width="39px"></asp:TextBox>
  113. =
  114. <asp:Label ID="Label1" runat="server"></asp:Label><br />
  115. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="calculate" />
  116. </ContentTemplate>
  117. </asp:UpdatePanel>
  118. <div id="AlertDiv">
  119. <div id="AlertMessage">
  120. </div>
  121. <br />
  122. <div id="AlertButtons">
  123. <input id="OKButton" type="button" value="OK" runat="server" onclick="ClearErrorState()" />
  124. </div>
  125. </div>
  126. </div>
  127. </form>
  128. </body>
  129. </html>