default.aspx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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"
  65. OnAsyncPostBackError="ScriptManager1_AsyncPostBackError" runat="server" >
  66. <Scripts>
  67. <asp:ScriptReference Path="ErrorHandling.js" />
  68. </Scripts>
  69. </asp:ScriptManager>
  70. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  71. <ContentTemplate>
  72. <asp:TextBox ID="TextBox1" runat="server" Width="39px"></asp:TextBox>
  73. /
  74. <asp:TextBox ID="TextBox2" runat="server" Width="39px"></asp:TextBox>
  75. =
  76. <asp:Label ID="Label1" runat="server"></asp:Label><br />
  77. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="calculate" />
  78. </ContentTemplate>
  79. </asp:UpdatePanel>
  80. <div id="AlertDiv">
  81. <div id="AlertMessage">
  82. </div>
  83. <br />
  84. <div id="AlertButtons">
  85. <input id="OKButton" type="button" value="OK" runat="server" onclick="ClearErrorState()" />
  86. </div>
  87. </div>
  88. </div>
  89. </form>
  90. </body>
  91. </html>