PageRequestManager_isInAsyncPostBack.aspx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 DateTime LastUpdate
  6. {
  7. get
  8. {
  9. return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);
  10. }
  11. set
  12. {
  13. ViewState["LastUpdate"] = value;
  14. }
  15. }
  16. void Page_Load()
  17. {
  18. if (!IsPostBack)
  19. {
  20. LastUpdate = DateTime.Now;
  21. }
  22. }
  23. protected void SlowProcessClick_Handler(object sender, EventArgs e)
  24. {
  25. System.Threading.Thread.Sleep(10000);
  26. LastUpdate = DateTime.Now;
  27. }
  28. protected void FastProcessClick_Handler(object sender, EventArgs e)
  29. {
  30. LastUpdate = DateTime.Now;
  31. }
  32. </script>
  33. <html xmlns="http://www.w3.org/1999/xhtml">
  34. <head id="Head1" runat="server">
  35. <title>PageRequestManager get_inPostBack Example</title>
  36. <style type="text/css">
  37. body {
  38. font-family: Tahoma;
  39. }
  40. div.AlertStyle
  41. {
  42. background-color: #FFC080;
  43. top: 95%;
  44. left: 1%;
  45. height: 20px;
  46. width: 270px;
  47. position: absolute;
  48. visibility: hidden;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <form id="form1" runat="server">
  54. <div>
  55. <asp:ScriptManager ID="ScriptManager1" runat="server" />
  56. <script type="text/javascript" language="javascript">
  57. Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
  58. function CheckStatus(sender, arg)
  59. {
  60. var prm = Sys.WebForms.PageRequestManager.getInstance();
  61. if (prm.get_isInAsyncPostBack()) {
  62. arg.set_cancel(true);
  63. ChangeAlertDivVisibility('visible');
  64. setTimeout("ChangeAlertDivVisibility('hidden')", 1000);
  65. }
  66. }
  67. function ChangeAlertDivVisibility(visstring)
  68. {
  69. var adiv = $get('AlertDiv');
  70. adiv.style.visibility = visstring;
  71. }
  72. </script>
  73. <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="Server" >
  74. <ContentTemplate>
  75. <asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
  76. Last update:
  77. <%= LastUpdate.ToString() %>
  78. .
  79. <br />
  80. <asp:Button ID="Button1"
  81. Text="Submit for Slow Process"
  82. OnClick="SlowProcessClick_Handler"
  83. runat="server" />
  84. <asp:Button ID="Button2"
  85. Text="Submit for Fast Process"
  86. OnClick="FastProcessClick_Handler"
  87. runat="server" />
  88. <br />
  89. </asp:Panel>
  90. </ContentTemplate>
  91. </asp:UpdatePanel>
  92. <asp:Panel ID="AlertDiv"
  93. CssClass="AlertStyle"
  94. runat="server" >
  95. still processing previous request...
  96. </asp:Panel>
  97. </div>
  98. </form>
  99. </body>
  100. </html>