StopAsynchronousPostback.aspx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <%@ Page Language="C#" %>
  2. <%@ Import Namespace="System.Collections.Generic" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <script runat="server">
  6. protected void NewsClick_Handler(object sender, EventArgs e)
  7. {
  8. System.Threading.Thread.Sleep(3000);
  9. HeadlineList.DataSource = GetHeadlines();
  10. HeadlineList.DataBind();
  11. }
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!IsPostBack)
  15. {
  16. HeadlineList.DataSource = GetHeadlines();
  17. HeadlineList.DataBind();
  18. }
  19. }
  20. // Helper method to simulate news headline fetch.
  21. private SortedList GetHeadlines()
  22. {
  23. SortedList headlines = new SortedList();
  24. headlines.Add(1, "This is headline 1.");
  25. headlines.Add(2, "This is headline 2.");
  26. headlines.Add(3, "This is headline 3.");
  27. headlines.Add(4, "This is headline 4.");
  28. headlines.Add(5, "This is headline 5.");
  29. headlines.Add(6, "(Last updated on " + DateTime.Now.ToString() + ")");
  30. return headlines;
  31. }
  32. </script>
  33. <html xmlns="http://www.w3.org/1999/xhtml">
  34. <head id="Head1" runat="server">
  35. <title>Canceling Postback Example</title>
  36. <style type="text/css">
  37. body {
  38. font-family: Tahoma;
  39. }
  40. #UpdatePanel1{
  41. width: 400px;
  42. height: 200px;
  43. border: solid 1px gray;
  44. }
  45. div.AlertStyle {
  46. font-size: smaller;
  47. background-color: #FFC080;
  48. width: 400px;
  49. height: 20px;
  50. visibility: hidden;
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <form id="form1" runat="server">
  56. <div >
  57. <asp:ScriptManager ID="ScriptManager1" runat="server">
  58. <Scripts>
  59. <asp:ScriptReference Path="CancelPostback.js" />
  60. </Scripts>
  61. </asp:ScriptManager>
  62. <asp:UpdatePanel ID="UpdatePanel1" runat="Server" >
  63. <ContentTemplate>
  64. <asp:DataList ID="HeadlineList" runat="server">
  65. <HeaderTemplate>
  66. <strong>Headlines</strong>
  67. </HeaderTemplate>
  68. <ItemTemplate>
  69. <%# Eval("Value") %>
  70. </ItemTemplate>
  71. <FooterTemplate>
  72. </FooterTemplate>
  73. <FooterStyle HorizontalAlign="right" />
  74. </asp:DataList>
  75. <p style="text-align:right">
  76. <asp:Button ID="RefreshButton"
  77. Text="Refresh"
  78. runat="server"
  79. OnClick="NewsClick_Handler" />
  80. </p>
  81. <div id="AlertDiv" class="AlertStyle">
  82. <span id="AlertMessage"></span>
  83. &nbsp;&nbsp;&nbsp;&nbsp;
  84. <asp:LinkButton ID="CancelRefresh" runat="server">
  85. Cancel</asp:LinkButton>
  86. </ContentTemplate>
  87. </asp:UpdatePanel>
  88. </div>
  89. </form>
  90. </body>
  91. </html>